我对WCF服务很新。我有一个使用NetHttpBinding创建的WCF服务,我有一个Form应用程序作为此服务的客户端,我试图用一个简单的双向通信示例。
这是我的
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>
以下是我的表单申请详情:
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>
我尝试调试它,我可以看到我从客户端到WCF服务的调用成功,因为我也可以看到“Inside Do Work”。但我无法通过
向客户回电话“callback.test(”WebSockets的Hello“);” ,我在web.config中启用了跟踪视图日志记录,我在traceView中看到以下错误:“无法查找通道以接收传入消息。无法找到端点或SOAP操作”
你可以告诉我我能错过什么吗?任何帮助表示赞赏。先谢谢你,
Aalap ..