2

我对 WCF 服务非常陌生。我有 1 个使用 NetHttpBinding 创建的 WCF 服务,并且我有一个 Form 应用程序作为该服务的客户端,我正在尝试使用它制作一个简单的 2 路通信示例。

这是我的

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);
    }
}

测试服务.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");
        }
    }
}

网络配置:

<?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");
        }
    }
}

应用程序配置:

<?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("Hello by WebSockets");" ,我在 web.config 中启用了跟踪视图日志记录,我可以在 traceView 中看到以下错误:“无法查找通道以接收传入消息。找不到端点或 SOAP 操作”

你能否让我知道我可能会错过什么。任何帮助表示赞赏。

先感谢您,

啊啊啊..

4

0 回答 0