动态分配WCF服务的名称混淆

本文关键字:WCF 服务 动态分配 | 更新日期: 2023-09-27 17:59:03

我有一个服务器,它托管3个web服务。

接口/签名将始终保持不变。

我还有桌面应用程序。

当这个桌面应用程序在我的服务器上注册时,它会返回一个IP地址,用于连接到web服务。

这里的想法是,我可以有多个去关联的服务器,前50个客户端将被分配服务器#1和下一个50个服务器#2的ip地址,依此类推

为了实现这一点,我不得不在客户端绑定中"加倍"服务名称,我想知道为什么?

如果我们只专注于一个服务,这就是服务器上的接口文件:

[ServiceContract]
public interface ISync
{
    [OperationContract(IsOneWay = true)]
    void UploadMotion(byte[] jpegStream, string alias, Int16 camIndex, byte[] motionLog);
}

服务器上我的web.config中的绑定是:

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ThrottledBehavior">
          <serviceTimeouts transactionTimeout="0.00:00:30" />
          <serviceThrottling maxConcurrentCalls="64" maxConcurrentSessions="50" maxConcurrentInstances="1" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="Sync" behaviorConfiguration="ThrottledBehavior">
        <endpoint address="Sync.svc" binding="basicHttpBinding" bindingConfiguration="ThrottledHttpBindingEndPoint" contract="ISync" name="wsSyncerMotion" />
      </service>
    </services>    
    <bindings>
      <basicHttpBinding>
        <binding name="ThrottledHttpBindingEndPoint" messageEncoding="Mtom" receiveTimeout="00:00:01" sendTimeout="00:00:01" transferMode="Streamed" closeTimeout="00:02:00" openTimeout="00:02:00">
          <readerQuotas maxArrayLength="32768" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

在我的客户端桌面应用程序中,我的app.config文件中有以下绑定:

  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="wsSyncerMotion" messageEncoding="Mtom" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://www.a url.co.uk/Sync.svc/Sync.svc"
        binding="basicHttpBinding" bindingConfiguration="wsSyncerMotion"
        contract="wsSyncer.ISync" name="wsSyncerMotion" />
    </client>
  </system.serviceModel>

我的c#代码动态分配一个地址:

ChannelFactory<HomeEdition.wsSyncer.ISyncChannel> FactorySync = null;
HomeEdition.wsSyncer.ISyncChannel UpLoadSync = null;
EndpointAddress addressSync = new EndpointAddress('a new ip address' + "/sync.svc/sync.svc");
FactorySync = new ChannelFactory<HomeEdition.wsSyncer.ISyncChannel>("wsSyncerMotion", addressSync);
UpLoadSync = Shared.FactorySync.CreateChannel();
UpLoadSync.OperationTimeout = new TimeSpan(0, 0, 0, 0, 1000);

注意:

sync.svc/sync.svc

如果我把它简化为sync.svc,它就不起作用。

我看不出该怎么做…

动态分配WCF服务的名称混淆

只需更改端点地址:替换

<endpoint address="Sync.svc" binding="basicHttpBinding" bindingConfiguration="ThrottledHttpBindingEndPoint" contract="ISync" name="wsSyncerMotion" />

通过

<endpoint address="" binding="basicHttpBinding" bindingConfiguration="ThrottledHttpBindingEndPoint" contract="ISync" name="wsSyncerMotion" />

  <endpoint address="http://www.a url.co.uk/Sync.svc/Sync.svc"
    binding="basicHttpBinding" bindingConfiguration="wsSyncerMotion"
    contract="wsSyncer.ISync" name="wsSyncerMotion" />

通过

  <endpoint address="http://www.a url.co.uk/Sync.svc"
    binding="basicHttpBinding" bindingConfiguration="wsSyncerMotion"
    contract="wsSyncer.ISync" name="wsSyncerMotion" />

当您希望通过多个绑定公开同一服务时,WCF服务的基本URL的扩展非常有用。