When developing a new project, using asp.net core technology, the old project may be a WCF service developed using the .NET Framework 4, because the reconstruction cost is too high, it is necessary to use asp.net core to call the WCF service interface.
First, create a new two projects:
ASP.NET Core 3.1 project: dotnet-core-call-wcf .NET Framework 4 project (WCF service application): schools-wcf-service
Visual Studio 2019 wants to create a WCF service, can't find the template, solution:
Select Install more tools and features to open Visual Studio Installer. Select the Individual Components tab, scroll down to Development Activities, select Windows Communication Foundation, and click Modify.
WCF creates two interfaces as follows:
The interface implementation code is as follows:
A service contract is a collection of related service operations, and when we apply a ServiceContractAttribute on an interface, we give it a service contract attribute. However, for such a type, its idiom does not automatically become a contractual service operation, and only when the OperationContractAttribute feature is applied can the corresponding method member become a service operation that can be accessed by service call.
When you start the wcf project, it will automatically run WcfTestClient and try to call it as shown in the figure below:
Use ASP.NET Core projects to call WCF services
Right-click Connected Services, select the service you want to add a connection to, and select WCF service, as shown in the following image:
Service address filled: http://localhost:3705/Service1.svc
Possible errors:
Since it is impossible to write the WCF service address to death, add the following code to the appsettings.json file:
Edit the Startup.cs file and register the WCF service with the following code:
Call the wcf service on the HomeController controller, first constructor injection, the code is as follows:
Calling the WCF service:
Run the asp.net Core website, the effect is as shown below:
We try to modify the WCF service so that the interface returns the result after 3 minutes, and then restart asp.net core to call the WCF service, and the error is as follows:
AggregateException: One or more errors occurred. (The request channel timed out attempting to send after 00:01:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.) At this time, we need to modify the default timeout, customize the BasicHttpBinding binding, and re-edit the Startup.cs file, as follows:
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
|