3

I have .NET C♯ application with WCF. I have implemented simple hello World and read simple operation from database but I am getting wcf Async method automatically along with my method.

I have two question

1- How can remove if I don't want to implement from WCF services

2- If I want to, how I implement WCF Async Method

I have two method for HeloWorld but got additional methods

Standard Method

namespace App.Services.Contracts
{
[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    string GetMessage(string name);
 }
}

Async Method

namespace App.Services.Contracts
{
[ServiceContract]
 public interface IHelloWorldServiceAsyn
  {
    [OperationContract]
    Task<string> GetMessageAsyn(string name);
  }
}

Implementation of HelloWorld Services

 public class HelloWorldManager : IHelloWorldService, IHelloWorldServiceAsyn
{
    public String GetMessage(string name)
    {
        return "This is test Service Flow, Hello world from " + name + "!";
    }

    public async Task<string> GetMessageAsyn(string name)
    {
        var task = Task.Factory.StartNew(() =>
                                            {
                                                Thread.Sleep(1000);
                                                return "This is test Service Flow, Hello world from " + name + "!";
                                            });

        return await task.ConfigureAwait(false);
    }
}

Service Model Config

  <service name="App.Services.Managers.HelloWorldManager" behaviorConfiguration="DefaultServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8087/CreditUnionServices/HelloWorldServices" />
      </baseAddresses>
    </host>
    <endpoint address="HelloWorld" binding="wsHttpBinding" bindingConfiguration="DefaultBindingConfiguration" contract="App.Services.Contracts.IHelloWorldService"></endpoint>
    <endpoint address="HelloWorldAsyn" binding="wsHttpBinding" bindingConfiguration="DefaultBindingConfiguration" contract="App.Services.Contracts.IHelloWorldServiceAsyn"></endpoint>
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="DefaultMexBindingConfiguration" contract="IMetadataExchange"></endpoint>
  </service>

you can see below screen shot that I have duplication of Async method, how I make sure it do not generate automatically so that when I need, I do it myself

enter image description here

K.Z
  • 5,201
  • 25
  • 104
  • 240
  • Why do you want to create `async` method at WCF side? If you want to make async requests from your **client** (consumer), then these methods will be created automatically - there is no need to implement it as service-side. – Yeldar Kurmangaliyev Jun 20 '16 at 08:15
  • so is that mean I always going to have two methods one standard and one aysn! is there way to filter out??? – K.Z Jun 20 '16 at 08:17
  • Almost :) It means that you are always going to have one method - standard. If you use Visual Studio to generate service proxy client, then it will always create `Async` methods by itself. – Yeldar Kurmangaliyev Jun 20 '16 at 08:28

1 Answers1

3

There is no async in the wire protocol. Client and server cannot tell whether the remote side is implemented in an async or sync way. You only need one at each side and they can be different.

Implement the server only once. Don't have two methods or two interfaces.

On the client you can freely pick between sync and async methods. You can disable async methods in the service reference settings.

I have explained this here.

Community
  • 1
  • 1
usr
  • 168,620
  • 35
  • 240
  • 369