开发者

How to generate a proxy for a wcf service

开发者 https://www.devze.com 2022-12-20 12:43 出处:网络
How to generate a proxy, Here is my service: using System; // Service.cs file namespace FirstWcfService {

How to generate a proxy, Here is my service:

using System;

// Service.cs file
namespace FirstWcfService
{
    public class Service : IService
    {开发者_如何学JAVA
        #region IService Members

        public string Hello()
        {
            return ("Hello WCF");
        }

        #endregion
    }
}


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

Then, in Visual Studio's Solution Explorer, ping "Add Service Reference":

How to generate a proxy for a wcf service

In the dialog box that appears, type in your service address, and click on "Go":

How to generate a proxy for a wcf service

This should connect to your service, discover the metadata, and if all goes well, you'll see your service (the service contract and its methods) in the middle part of the screen:

How to generate a proxy for a wcf service

Before you click on "OK" too quickly - pay attention to the textbox "Namespace" in the lower left corner - you can type in a namespace in which your service reference (the classes it generates) will live. I typically use something like (project).(servicename).Adapter - choose whatever makes sense to you.

Now, in your Solution Explorer, you'll see a new icon for the service you've just referenced - when you click on the "Show all files" button on the Solution Explorer toolbar, you'll see all the files that were generated. The one where your classes live is always called Reference.cs.

How to generate a proxy for a wcf service

When you dare to open that file :-), you'll see that you'll have a class called (yourservicename)Client which is what you need to instantiate in your client code - it will carry all the defined service methods, which you can now call from your code:

How to generate a proxy for a wcf service

Hope this helps !


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

Option one is to use the auto generated object

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

Option 2 is to use the channel factory

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

client.DoAwesomeStuff();

This is a pretty informative blog post you might like to read on when and why to use each of these methods. This screencast will help you too.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号