silverlight应用程序根本不会查看托管服务器的web.config - 这是在服务器端,并且对客户端上运行的Silverlight应用程序不可见。当您在代码中创建本地服务代理时,Silverlight应用程序将在它自己的ServiceReferences.clientconfig文件中或在您以编程方式指定的URL中查找。
因此,您有2个选项:
1.在构建可部署版本的Silverlight应用程序之前,修改ServiceReferences.clientconfig。
2.使用代码构建您的客户端端点的URL。
我们使用第二个选项,因为我们希望有一个标准的编程接口来配置我们的端点。我们做这样的事情(但不与的MaxValue的,如果它是一个面向公众的服务,当然):
public ImportServiceClient CreateImportServiceClient()
{
return new ImportServiceClient(GetBinding(), GetServiceEndpoint("ImportService.svc"));
}
public ExportServiceClient CreateExportServiceClient()
{
return new ExportServiceClient(GetBinding(), GetServiceEndpoint("ExportService.svc"));
}
protected override System.ServiceModel.Channels.Binding GetBinding()
{
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
binding.MaxBufferSize = int.MaxValue;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.SendTimeout = TimeSpan.MaxValue;
binding.ReceiveTimeout = TimeSpan.MaxValue;
return binding;
}
protected EndpointAddress GetServiceEndpoint(string serviceName)
{
if (Settings == null)
{
throw new Exception("Service settings not set");
}
return
new EndpointAddress(ConcatenateUri(Settings.ServiceUri,serviceName));
}
protected EndpointAddress GetServiceEndpoint(string serviceName, Uri address)
{
if (Settings == null)
{
throw new Exception("Service settings not set");
}
return new EndpointAddress(ConcatenateUri(address, serviceName));
}
像“ImportServiceClient”和“ExportServiceClient”的类是从创建服务引用到生成的代理我们的WCF服务。 Settings.ServiceUri是我们存储应该与之交谈的服务器地址的地方(在我们的例子中,它通过参数动态设置到托管页面中的silverlight插件,但是您可以使用任何您喜欢的方案来管理这个地址)。
但是,如果你喜欢简单地调整ServiceReferences.ClientConfig,那么你不需要任何这样的代码。
@Clay:谢谢兄弟!有效。 – pencilslate