Reputation: 2917
I am very new to WCF services. I have 1 WCF service created with NetHttpBinding and I have a Form application as a client of this service and I am trying to make a simple example of 2 way communication using this.
Here is my
ITestService.cs
namespace TestService
{
[ServiceContract(CallbackContract = typeof(ITestServiceCallback), SessionMode = SessionMode.Required)]
public interface ITestService
{
[OperationContract]
void DoWork();
}
[ServiceContract]
public interface ITestServiceCallback
{
[OperationContract(IsOneWay = true)]
void test(string hello);
}
}
TestService.svc.cs:
namespace TestService
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class TestService : ITestService
{
public void DoWork()
{
Debug.WriteLine("Inside Do Work");
ITestServiceCallback callback = OperationContext.Current.
GetCallbackChannel<ITestServiceCallback>();
Debug.WriteLine(OperationContext.Current.Channel.RemoteAddress.ToString());
callback.test("Hello by WebSockets");
}
}
}
web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<bindings>
<netHttpBinding>
<binding name="TextOverWebSockets" messageEncoding="Text"/>
</netHttpBinding>
</bindings>
<services>
<service name="TestService.TestService"
behaviorConfiguration="TestServiceBehavior">
<endpoint address=""
binding="netHttpBinding"
bindingConfiguration="TextOverWebSockets"
contract="TestService.ITestService" >
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TestServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="netHttpBinding" scheme="http" />
<add binding="netHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Here is my form application details :
Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TestServiceClient Client = new TestServiceClient(new InstanceContext(new CallbackHandler()));
Debug.WriteLine(Client.Endpoint.Address.ToString());
Debug.WriteLine("asd Startttttt");
Client.Open();
Client.DoWork();
}
public class CallbackHandler : ITestServiceCallback
{
public void test(string hello)
{
Debug.WriteLine("asd Finallllly");
}
}
}
App.config :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<netHttpBinding>
<binding name="NetHttpBinding_ITestService" messageEncoding="Text">
<webSocketSettings transportUsage="Always" />
</binding>
</netHttpBinding>
</bindings>
<client>
<endpoint address="ws://localhost:64375/TestService.svc" binding="netHttpBinding"
bindingConfiguration="NetHttpBinding_ITestService" contract="TestService.ITestService"
name="NetHttpBinding_ITestService">
</endpoint>
</client>
</system.serviceModel>
</configuration>
I tried debugging it and I could see that My call from Client to WCF service succeeds cause I could see "Inside Do Work" also. But I was not able to make the call back to the Client at
"callback.test("Hello by WebSockets");" , I had enabled the trace view logging in web.config and I could see following error in traceView : "Failed to lookup a channel to receive an incoming message. Either the endpoint or the SOAP action was not found"
Could you please let me know what could I be missing. Any help is appreciated.
Thank you in Advance,
Aalap..
Upvotes: 2
Views: 2241