Get the App
SLTechnology News&Howtos  ›  Development  › 

How to create a proxy that uses WCF

Shulou Source: shulou.com Published: 2022-06-02 18:37:11 09月16日 Update

This article mainly shows you "how to create a WCF agent", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to create a WCF agent" this article.

The WCF agent is still quite commonly used, so I studied the WCF agent and shared it with you here. I hope it will be useful to you. The proxy class generated by SvcUtil is derived from the class ClientBase and is defined as:

Public class ClientBase: IDisposable {protected ClientBase (string endpointConfigurationName); protected ClientBase (Binding binding,EndpointAddress remoteAddress); public void Dispose (); protected T InnerProxy {get;} / / other members}

This InnerProxy attribute is the type of contract that the client needs to consume, and a subclass of ClientBase generated by SvcUtil simply WCF it to the method call. The client needs to instantiate a proxy object and provide it with constructor endpoint information-either the endpoint section name from the configuration file or the endpoint address and binding object when not using a configuration file. The client can then use the proxy method to invoke the service, and when the client is finished, it needs to close the proxy instance:

MyContractProxy proxy = new MyContractProxy ("MyEndpoint"); proxy.MyMethod (); proxy.Close ()

Shutting down the WCF agent terminates the session with the service and closes the connection. Alternatively, you can use the agent's Dispose () method to turn it off. The advantage of the Dispose () method is that you can use the using statement to call it, even when faced with exception handling:

Using (MyContractProxy proxy = new MyContractProxy ("MyEndpoint")) {proxy.MyMethod ();}

One endpoint for each contract type in the client profile can be assigned as a default endpoint. The default endpoint is an endpoint section with no name flag or only an empty name (""):

A default endpoint is only ideal; when creating a proxy for the default endpoint, you can use the default constructor of the WCF proxy to make it use the default endpoint:

MyContractProxy proxy = new MyContractProxy (); proxy.MyMethod (); proxy.Close (); that's all of the article "how to create and use WCF agents". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Agents endpoints clients clients methods content files articles configurations contracts instances objects types constructors learning help services generation research advantages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi macOS MariaDB MySQL