How to create a proxy for wcf service

How to create a proxy server, here is my service:

using System; // Service.cs file namespace FirstWcfService { public class Service : IService { #region IService Members public string Hello() { return ("Hello WCF"); } #endregion } } 
+4
source share
2 answers

First of all, make sure that your service you want to refer to is up and running.

Then, in the Visual Studio Solution Explorer, run the ping "Add Service Link" ping command:

alt text

In the dialog that appears, enter the address of your service and click "Go":

alt text

This should connect to your service, find metadata, and if everything goes well, you will see your service (service contract and its methods) in the middle of the screen:

alt text

Before you click "OK" too quickly - pay attention to the "Namespace" text box in the lower left corner - you can enter the namespace in which your link to the service (the classes generated by it) will live. Usually I use something like (project).(servicename).Adapter - choose what makes sense to you.

Now, in the Solution Explorer, you will see a new icon for the service you just referred to - when you click the "Show All Files" button on the Solution Explorer toolbar, you will see all the files created. The place where your classes live is always called Reference.cs .

alt text

When you dare to open this file :-), you will see that you will have a class with the name (yourservicename)Client that you need to create in your client code - it will contain all the specific service methods that you can now call from your code:

alt text

Hope this helps!

+9
source

After you have configured access to your WCF service, you have two options:

Option one - use an automatically generated object

 var proxy = new MyServiceProxyClient(); proxy.open(); //do work proxy.close(); 

Option 2 - use the factory channel

 ChannelFactory<IMyService> channel = new ChannelFactory<IMyService>("bindingNameFromYourConfigFile"); IMyService client = channel.CreateChannel(); client.DoAwesomeStuff(); 

This is a pretty informative blog post that you could read about when and why to use each of these methods. This screencast will also help you.

+2
source

Source: https://habr.com/ru/post/1301121/


All Articles