.Net Core下使用WCF—— Consuming WCF Services in .NET Core – Best Practices
原文:https://thecodebuzz.com/consuming-wcf-web-services-in-net-core-best-practices/
Consuming WCF Services in .NET Core – Best Practices
Today in this article of best practices, we shall learn how to consume WCF services easily in .NET Core based application( like .Net Core WebAPI or ASP.NET Core web applications or any other type of projects)
Today we shall cover below techniques of consuming WCF services in .NET or ASP.NET core applications,
- Connected Service in Visual Studio IDE
- ServiceModel Metadata Global CLI tools(svcutil.exe)
- Using WSDL to create a proxy
- Using Channel Factory approach
- Enable existing WCF services as REST API
WCF is a rich programming platform for building service-oriented applications. Today seamless integration of application in an Enterprise is a standard and the recent popularity of the framework like RESTful services (WebAPI), It is possible that an organization may need existing WCF or Web legacy services integrated with modern frameworks and technologies.
Connecting WCF service from .NET Core
To use WCF services in .NET Core, you need to create a proxy client.
Proxy is actually a contract equivalent of actual service and contains complete details of the Interface and method exposed by WCF service.
One can also use the Channel factory technique to connect to the WCF service easily.
Ways to consume WCF WebServices in .NET Core
We shall see below 3 ways to consume WCF services in .NET Core
- Connected Service in Visual Studio( VS2017 and above)
- Using ServiceModel Metadata Global CLI tool
- Using Channel Factory
Getting Started
To get started, you certainly need a WCF service. I already have a sample WCF service with contract details as below. This service we will be consuming within the .NET Core WebAPI application.
However, you can use the below technique for other types of applications as well like .NET Core Console or Form application (.NET Core 3.0) or ASP.NET Core MVC, etc.
WCF Service Contract
This service has a method GetOrderData() which returns an order data for a given order ID input.
Create ASP.NET Core 2.0 or 3.1 API
Let’s start creating a WebAPI application (you can create any other type of project as required.)
1. Using Connected Services
This is a very simple technique of creating a proxy within the target application. This option is available in .NET Core or .NET Standards.
Connected service lets you create client-side code easily using direct Service URI (If available) or using WSDL file (If shared by the vendor or third party).
I have discussed both approaches in detail which are based on ‘Connected Services’ using WSDL and URI.
- Configure WCF web service reference using URI
- Configure WCF web service reference using WSDL file
Note: Connected service option is more stable in VS2017 (15.8.0) and above versions only. For the lower version, this option may or may not work. If you come across any issue please try using the 2nd preferred approach- Using ServiceModel MetadataCLI tool which can be used for both VSCode and VS2017 IDE based application.
Right-click on “Connected services” as below. This option will be available for all .NET Core or .NET Standards library as given below
Or
Please select the option “Microsoft WCF Web Service…” as highlighted below,
Configure WCF web service reference using URI
Let’s look at how to create client code for .NET Core using the service URL.
As shown below please enter the URI as below and click on ‘Next’.
I got the WCF service URL from the sample service as below.
This URL will be generally in the format of .svc. You shall be entering your actual provider/client URL.
Once you click ‘finish’ you shall see below windows on the progress of service metadata generation and scaffolding etc.
Post successful completion of the above steps, the service reference will be added as below in .NET Core WebAPI
This Reference.cs will have a client class as highlighted below which needs to be instantiated and used to connect with the WCF Service,
Lets now go to WebAPI method and use the class OrderServiceClient to call the actual WCF service and get the results.
As a good practice put this proxy generated class in a proper layer of service. You may consider ‘IntegrationLayer’ or ‘InfraLayer’ repository etc.
The file can also be modified for endpoint configuration to support multiple URLs or proxy and the same can be provided through Configuration in .NET Core or Environment variables etc.
Here is the successful result finally from a WCF service within .NET Core WebAPI,
You are all set !!
If you only have access to the WSDL definition, then please use the below techniques to create client code.
Configure WCF web service reference using WSDL
If you have access to WSDL file (Web Services Description Language) then the same above technique can also be used to create client code for .NET Core.
A typical look of .wsdl will be a below,
Please use the same connected service configuration and browse through WSDL file as shown below,
Using WSDL Schema for Proxy generation
Click on Next and choose the option as appropriate.
Finally, you shall see proxy scaffolding generation which is already explained above.
Please follow the same above steps to connect a WCF service from .NET Core app.
Happy Coding !!!
2. Using ServiceModel Metadata (svcutil.exe) Global Tool
The above option of ‘Connected Services‘ was very easy and simple to use.
However, this UI extension will make you fuzzy due to dependency on the right version (Visual Studio 2017, and the below versions are not stable.Works fine in VS2019 and above version).
SVC-UTIL utility available as NuGet package generates proxy(client-code) using URI and WSDL both approaches. Please see below article for more details on this approach,
If you using other IDE like VSCode or looking for global working cross-platform on Linux, macOS, and Windows then you can use this option for creating client-side code.
3. Using Channel Factory Method
Don’t want to create/maintain proxy? Yes ..then this could be one of the preferred approach if you don’t want to maintain a proxy code. Please see below article for more details on this approach,
4. Enable existing WCF services as REST API
If you need to enable WCF services for additional endpoints like a REST API endpoint you can do easily so that it can be consumed across any type client
The good news is you can do it without breaking any client.
This technique will give you breather time if budget or capacity etc. issues are hindering you.
Summary
WCF Web Service integration with modern technologies like .NET Core services is possible. In this post, we learned three possible approaches like using,
- ‘Connected Services’ using Visual Studio
- ‘ServiceModel Metadata (svcutil.exe)’ a CLI utility and
- Channel Factory approach.
First, two techniques use proxy scaffolding of service contracts and last techniques make use of ChannelFactory thereby allowing connecting .NET Core application with WCF Web services.
Thank you for reading. Please let me know your questions, thoughts or feedback below in the comments section. I appreciate your feedback and encouragement.
参考:
.Net Core下使用WCF
在.net core 环境中用WCF全是异步的,自动会将你的wcf服务提供的同步方法变为异步方法
var WcfClient=new WcfTestService.WcfTestServiceClient(); //在这里我将异步方法转化为了同步的方法 var result=WcfClient.TestAsync(name).Result; //如果这时返回Result 会报错 ;无法将WcfTestService.a的类型转化为CNBlogs.Job.A.a的类型 //我们需要用到AutoMapper进行一个类型的转化,这样才不会报错 AutoMapper.Mapper.Initialize(c=>c.CreateMap<WcfTestService.a,,CNBlogs.Job.A.a>()); return AutoMapper.Mapper.Map<CNBlogs.Job.A.a>(result);
WCF 客户端调用几种方式