开发者

How to implement IsOneWay=true in WCF nettcpBinding

How can I implement one way WCF operations?

I just tried using IsOneWay attribute as:

[OperationContract(IsOneWay=true)]
void MethodName(string param1, int param2)

Is there any other change I need to make or any specific cha开发者_如何学Gonge in app.config?

FYI, my WCF service implements netTcpBinding, though I think that shouldn't make any difference.


As shown, your code looks ok. There should be no problem with doing one-way calls with netTcpBinding.

If you're interested, chapter 5 in Juval Lowy's awesome Programming WCF Services 2nd Edition contains a good bit of information about one-way services.

From what you've shown, so far though I don't see anything wrong. Please give us some more details.


We had a problem with one-way calls not returning immediately using the NetTcpBinding. This blog post identifies the problem and provides a solution.

http://blogs.msdn.com/b/distributedservices/archive/2009/02/12/client-proxy-close-method-call-does-not-finish-immediately-in-one-way-wcf-calls.aspx

From the article:

Problem: Clients calling a one-way method in WCF Service and then close method on proxy does not return until the call is actually finished or call times out. Ever wonder why this happens?

Cause: When you specify “One-Way” on your interface, the underlying channel operation is still two-way since the one way binding element is not in the channel stack. Thus, the close operation gets blocked until the one way operation completes.

This is by design and the development team is working to change it in future versions of .Net framework.

...

Solution (Work around):

Layer the OneWayBindingElement on top of netTcpBinding as shown in the below code. This way, close call on proxy will return immediately and eventually the one-way call will return in fire and forget fashion.

[ServiceContract]
public interface IService1
{
    [OperationContract(IsOneWay = true)]
    void SetData(int value);
}

public class Service1 : IService1
{
    public void SetData(int value)
    {
         //Application specific code
    }
}

Service Host code:

        Form1ServiceHost = new ServiceHost(this, new Uri("net.tcp://localhost:8091/WindowsFormApp/Form1/"), new Uri("http://localhost:8090/WindowsFormApp/Form1/"));

        Binding binding = new NetTcpBinding();
        BindingElementCollection oldBindingElements = binding.CreateBindingElements();
        BindingElementCollection bindingElements = new BindingElementCollection();
        bindingElements.Add(new OneWayBindingElement());
        foreach (BindingElement bindingElement in oldBindingElements)
        {
            bindingElements.Add(bindingElement);
        }

        binding = new CustomBinding(bindingElements);

        Form1ServiceHost.AddServiceEndpoint("WCFServiceLibrary.IService1", binding, "");
        Form1ServiceHost.Open();

Client Code:

 Binding binding = new NetTcpBinding();
        BindingElementCollection oldBindingElements = binding.CreateBindingElements();
        BindingElementCollection bindingElements = new BindingElementCollection();
        bindingElements.Add(new OneWayBindingElement());
        foreach (BindingElement bindingElement in oldBindingElements)
        {
            bindingElements.Add(bindingElement);
        }

        binding = new CustomBinding(bindingElements);

        Service1Client client = new Service1Client(binding, new EndpointAddress("net.tcp://localhost:8091/WindowsFormApp/Form1/"));
        client.SetData(10);
        Console.WriteLine("set data");
        Console.WriteLine("Now closing the channel,Before close, current time is {0}", DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString());
        client.Close();
        Console.WriteLine("Now closing the channel,After close, current time is {0}", DateTime.Now.ToString() + " " + DateTime.Now.Millisecond.ToString());`
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜