WCF在代码中动态设置Endpoint和Binding

Can*_*ğlu 5 wcf web-services webservice-client wcfserviceclient silverlight-4.0

是的,我已经阅读了有关SO,MSDN和其他网站的其他问题,但我发现没有我能理解的答案.我需要设置我的Silverlight应用程序的WCF引用相对于它加载的站点,但我无法让它工作.服务本身没有问题,它正在运行.当我从本地移动到我的真实服务器时,我的SL应用程序中出现错误,抱怨没有连接到localhost.

这是我的ServiceReferences.ClientConfig档案:

    <configuration>
        <system.serviceModel>
            <bindings>
                <customBinding>
                    <binding name="CustomBinding_AccountManager">
                        <binaryMessageEncoding />
                        <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                    </binding>
                    <binding name="CustomBinding_FileManager">
                        <binaryMessageEncoding />
                        <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                    </binding>
                    <binding name="CustomBinding_SiteManager">
                        <binaryMessageEncoding />
                        <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
                    </binding>
                </customBinding>
            </bindings>
            <client>
                <endpoint address="http://localhost:60322/AccountManager.svc"
                    binding="customBinding" bindingConfiguration="CustomBinding_AccountManager"
                    contract="AccountManager.AccountManager" name="CustomBinding_AccountManager" />
                <endpoint address="http://localhost:60322/FileManager.svc" binding="customBinding"
                    bindingConfiguration="CustomBinding_FileManager" contract="FileManager.FileManager"
                    name="CustomBinding_FileManager" />
                <endpoint address="http://localhost:60322/SiteManager.svc" binding="customBinding"
                    bindingConfiguration="CustomBinding_SiteManager" contract="SiteManager.SiteManager"
                    name="CustomBinding_SiteManager" />
            </client>
        </system.serviceModel>
    </configuration>
Run Code Online (Sandbox Code Playgroud)

是的,我优化缓冲区/消息大小,我知道可能的DoS漏洞,暂时忘记它,我需要它们进行大文件传输.我尝试的方法是在实例化客户端时,我使用了这段代码:

    fileManager = new FileManagerClient(new BasicHttpBinding(), new EndpointAddress("http://" + Settings.Host + "/FileManager.svc"));
    accManager = new AccountManagerClient(new BasicHttpBinding(), new EndpointAddress("http://" + Settings.Host + "/AccountManager.svc"));
Run Code Online (Sandbox Code Playgroud)

Settings.Host我自己的方法在哪里,它返回SL应用程序运行的主机,测试,工作.当我上传我的XAP并尝试时,它仍然想要进行http://localhost:60322/AccountManager.svc,经过进一步调查后,我意识到仍有很多对localhost 引用,在看不见的文件中:

AccountManager.disco:

<?xml version="1.0" encoding="utf-8"?>
<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">
  <contractRef ref="http://localhost:60322/AccountManager.svc?wsdl" docRef="http://localhost:60322/AccountManager.svc" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
</discovery>
Run Code Online (Sandbox Code Playgroud)

部分AccountManager.wsdl: ... <wsdl:import namespace="" location="http://localhost:60322/AccountManager.svc?wsdl=wsdl0" />... ... [许多操作声明] ......

<wsdl:service name="AccountManager">
    <wsdl:port name="CustomBinding_AccountManager" binding="tns:CustomBinding_AccountManager">
      <soap12:address location="http://localhost:60322/AccountManager.svc" />
      <wsa10:EndpointReference>
        <wsa10:Address>http://localhost:60322/AccountManager.svc</wsa10:Address>
      </wsa10:EndpointReference>
    </wsdl:port>
  </wsdl:service>
Run Code Online (Sandbox Code Playgroud)

部分AccountManager1.xsd:

<xs:import schemaLocation="http://localhost:60322/AccountManager.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/Leftouch.Data.Summary" />

部分configuration.svcinfo:

<endpoint normalizedDigest="&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;&lt;Data address=&quot;http://localhost:60322/AccountManager.svc&quot; binding=&quot;customBinding&quot; bindingConfiguration=&quot;CustomBinding_AccountManager&quot; contract=&quot;AccountManager.AccountManager&quot; name=&quot;CustomBinding_AccountManager&quot; /&gt;" digest="&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-16&quot;?&gt;&lt;Data address=&quot;http://localhost:60322/AccountManager.svc&quot; binding=&quot;customBinding&quot; bindingConfiguration=&quot;CustomBinding_AccountManager&quot; contract=&quot;AccountManager.AccountManager&quot; name=&quot;CustomBinding_AccountManager&quot; /&gt;" contractName="AccountManager.AccountManager" name="CustomBinding_AccountManager" />
Run Code Online (Sandbox Code Playgroud)

部分Reference.svcmap:

</ClientOptions>
  <MetadataSources>
    <MetadataSource Address="http://localhost:60322/AccountManager.svc" Protocol="http" SourceId="1" />
  </MetadataSources>
  <Metadata>
    <MetadataFile FileName="AccountManager2.xsd" MetadataType="Schema" ID="e473b2d5-7af3-4390-87c3-a4fc3f54fb96" SourceId="1" SourceUrl="http://localhost:60322/AccountManager.svc?xsd=xsd2" />
    <MetadataFile FileName="AccountManager1.xsd" MetadataType="Schema" ID="fd3a1ae0-b38b-4586-8622-5b0ee07e39fb" SourceId="1" SourceUrl="http://localhost:60322/AccountManager.svc?xsd=xsd0" />
    <MetadataFile FileName="AccountManager.xsd" MetadataType="Schema" ID="6a49ee64-6eac-40e2-bcff-26418435e777" SourceId="1" SourceUrl="http://localhost:60322/AccountManager.svc?xsd=xsd1" />
    <MetadataFile FileName="AccountManager.disco" MetadataType="Disco" ID="9ec9a8cc-0cf0-4264-a526-b5a6c08f7d36" SourceId="1" SourceUrl="http://localhost:60322/AccountManager.svc?disco" />
    <MetadataFile FileName="AccountManager1.wsdl" MetadataType="Wsdl" ID="54a5b2c0-9d0e-4043-a7e4-d27ae6674bfc" SourceId="1" SourceUrl="http://localhost:60322/AccountManager.svc?wsdl=wsdl0" />
    <MetadataFile FileName="AccountManager.wsdl" MetadataType="Wsdl" ID="f8923013-3a6c-412b-b7da-bee5a5a7bb64" SourceId="1" SourceUrl="http://localhost:60322/AccountManager.svc?wsdl" />
Run Code Online (Sandbox Code Playgroud)

..还有所有这些也适用于其他2项服务.

我不是Web服务/绑定/端点/操作合同或任何相关内容的主人.我只是想让我完全已经很好的工作(当URI是硬编码的)系统工作的相对URI,这就是我所需要的.必须有一个简单的解决方案.有人可以解释这些文件类型和声明的确切含义,哪些是重要的,哪些是可选的,以及如何以最干净的形式创建动态服务引用.请解释一下.我已经看过很多关于动态服务绑定和引用的帖子和文章,但老实说,一切都搞砸了,我最终都不理解它.任何建设性的批评和解决方案都是受欢迎的.

Tim*_*Tim 2

您的服务(而不是客户端)的配置文件是否使用具有完全限定地址的端点(例如您的客户端配置)?如果是这样,您看到所有这些本地主机引用的原因是因为当您添加服务引用时,它将在生成的文件中携带该地址。

更新服务配置文件中的端点以使用该计算机上的完全限定地址(即http://somname.com/service.svc),或将配置文件中的 baseAddresses 设置为计算机名称。

此外,如果您使用的是 WCF 4.0 (VS 2010/.NET 4.0),您可以让 WCF 通过完全忽略配置文件中的端点来为您的服务创建默认端点(据我所知 - 我们是现在工作中刚刚迁移到 4.0,所以我还没有使用新功能)。

编辑添加

新方法,相同的基本思想(URI 是从某个地方导入的)。根据您下面的评论,听起来您的服务配置设置得很好,没有硬编码的 URI 指向 localhost。

当您将应用程序移动到目标服务器时,您是否也会更新对服务的引用(通过添加服务引用),或者只是将本地生成的文件移动到目标服务器?

如果是的话,我想知道这是否可能是您问题的根源。我认为在创建客户端时指定服务地址应该覆盖 WSDL 相关文件中的任何内容,但也许不是。

值得尝试的事情:

<client>从 Web.config 中删除该部分。然后,当您创建客户端时,请执行以下操作:

fileManager = new FileManagerClient(new BasicHttpBinding("CustomBinding_FileManager"), new EndpointAddress("http://" + Settings.Host + "/FileManager.svc"));
Run Code Online (Sandbox Code Playgroud)

确保在构造函数中传入绑定配置节的名称BasicHttpBinding,否则您将获得具有默认值的绑定,而不是您指定的较大值。

这里的想法是消除客户端配置文件设置覆盖您在 FileManagerClient 创建中传递的内容的任何可能性。

我认为必须更新每个部署到每个单独服务器的服务引用不太理想 - 您想要完成的任务是有意义的。我在我编写的 n 层应用程序中做了类似的事情 - 唯一的区别是我不使用服务引用,我通过 SvcUtil 生成代理,然后通过 生成通道ChannelFactory<T>,这是您可能想要查看的另一条路线。

如果这些都没有帮助,请随时给我发电子邮件(我的电子邮件地址在我的个人资料中) - 通过电子邮件交换解决这个问题可能会更容易,然后发布最终的解决方案。