WCF在代码中动态设置端点和绑定

WCF在代码中动态设置端点和绑定,wcf,web-services,silverlight-4.0,webservice-client,wcfserviceclient,Wcf,Web Services,Silverlight 4.0,Webservice Client,Wcfserviceclient,是的,我已经阅读了SO、MSDN和其他网站上的其他问题,但我没有找到尽可能清晰的答案。我需要将Silverlight应用程序的WCF引用设置为相对于加载它的站点的引用,但我无法让它工作。服务本身没有问题,它正在工作。当我从本地移动到真正的服务器时,我的SL应用程序中出现错误,抱怨没有连接到本地主机 这是我的servicerences.ClientConfig文件: <configuration> <system.serviceModel>

是的,我已经阅读了SO、MSDN和其他网站上的其他问题,但我没有找到尽可能清晰的答案。我需要将Silverlight应用程序的WCF引用设置为相对于加载它的站点的引用,但我无法让它工作。服务本身没有问题,它正在工作。当我从本地移动到真正的服务器时,我的SL应用程序中出现错误,抱怨没有连接到本地主机

这是我的
servicerences.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>
其中
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>
AccountManager1.xsd的一部分

配置的一部分。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" />
..其他2项服务也同样如此


我不是web服务/绑定/端点/操作契约或任何相关内容的大师。我只想让我已经很好的工作(当URI是硬编码的)系统为相对URI工作,这就是我所需要的。必须有一个简单的解决办法。有人能解释一下这些文件类型和声明到底是什么样的,哪些是重要的,哪些是可选的,以及我如何以最干净的形式创建动态服务引用。请解释一下。我已经看过很多关于动态服务绑定和引用的帖子和文章,但老实说,一切都搞砸了,最终我什么都不懂。欢迎任何建设性的批评和解决方案。

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

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

此外,如果您使用的是WCF 4.0(VS 2010/.NET 4.0),您可以让WCF通过从配置文件中完全匹配端点来为您的服务创建一个默认端点(据我所知,我们现在只是在使用4.0,所以我还没有使用新功能)

编辑以添加

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

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

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

尝试一下:

从Web.config中删除
部分。然后,在创建客户机时,按如下方式执行:

fileManager = new FileManagerClient(new BasicHttpBinding("CustomBinding_FileManager"), new EndpointAddress("http://" + Settings.Host + "/FileManager.svc"));
确保在
BasicHttpBinding
构造函数中传入绑定配置部分的名称,否则将获得具有默认值的绑定,而不是指定的较大值

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

我认为将每个服务部署的服务引用更新到每个单独的服务器是不太理想的。我在我编写的一个n层应用程序中也做了类似的事情——唯一的区别是我不使用服务引用,我通过SvcUtil生成代理,然后通过

ChannelFactory
生成频道,这是您可能想查看的另一条路线


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

你能发布你正在使用的服务配置文件吗?你到底在说什么文件?我查看了我的web.config和.svc标记,并没有关于域的内容。顺便说一句,是的,我使用的是.NET4.0,我指的是服务的Web.config文件。您对该文件中的端点有什么要求?
和2个具有其他服务名称的副本。您是否在服务的配置中指定了任何基本地址或URI?我应该发布什么?我的服务是我网站的一部分,我已经粘贴了相关部分。我不明白你还问什么。我没有在任何地方找到和硬编码的URI,但我肯定可能遗漏了一些东西。只要准确地告诉我你要什么文件的哪一部分,我会粘贴它
<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" />
</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" />
fileManager = new FileManagerClient(new BasicHttpBinding("CustomBinding_FileManager"), new EndpointAddress("http://" + Settings.Host + "/FileManager.svc"));