在自托管服务中,我想使用 App.config 中指定的端点(如果存在),或者如果 App.config 为空,则使用代码中指定的默认端点。我怎样才能做到这一点?
编辑:澄清一下,这是在使用 ServiceHost 的服务器(服务)端。
在自托管服务中,我想使用 App.config 中指定的端点(如果存在),或者如果 App.config 为空,则使用代码中指定的默认端点。我怎样才能做到这一点?
编辑:澄清一下,这是在使用 ServiceHost 的服务器(服务)端。
当我想在没有 app.config 文件的情况下实现独立服务客户端时,我遇到了同样的问题。最后我可以解决它。请遵循以下代码示例。它工作正常,我已经测试过了。
BasicHttpBinding binding = new BasicHttpBinding();
binding.Name = "BasicHttpBinding_ITaskService";
binding.CloseTimeout = TimeSpan.FromMinutes(1);
binding.OpenTimeout = TimeSpan.FromMinutes(1);
binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
binding.SendTimeout = TimeSpan.FromMinutes(1);
binding.AllowCookies = false;
binding.BypassProxyOnLocal = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.MaxBufferSize = 65536;
binding.MaxBufferPoolSize = 524288;
binding.MaxReceivedMessageSize = 65536;
binding.MessageEncoding = WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.TransferMode = TransferMode.Buffered;
binding.UseDefaultWebProxy = true;
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
binding.Security.Transport.Realm = "";
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
Uri endPointAddress = new Uri("http://www.kapanbipan.com/TaskService.svc");
ChannelFactory<taskmgr.TaskService.ITaskServiceChannel> factory = new ChannelFactory<ITaskServiceChannel>(binding, endPointAddress.ToString());
taskmgr.TaskService.ITaskServiceChannel client = factory.CreateChannel();
一种方法是try
第一次尝试从配置文件加载,并将端点硬编码在catch
. 例如:
MyServiceClient client = null;
try
{
client = new MyServiceClient();
}
catch (InvalidOperationException)
{
EndpointAddress defaultAddress = new EndpointAddress(...);
Binding defaultBinding = new Binding(...);
client = new MyServiceClient(defaultBinding, defaultAddress);
}
您可以获得配置部分,如下所示:
var clientSection = System.Configuration.ConfigurationManager.GetSection("system.serviceModel/client");
如果 value 为 null 或clientSection.Endpoints
包含零个元素,则未定义。
试试这个......未经测试,但应该适合你。它将检查您的配置以查找具有匹配合同的任何端点。您可以将其更改为按名称匹配、返回不同的信息或任何对您的情况有意义的内容。如果没有找到匹配项,您可以放入逻辑以创建默认端点。
public List<EndpointAddress> GetEndpointAddresses(Type t)
{
string contractName = t.FullName;
List<EndpointAddress> endpointAddresses = new List<EndpointAddress>();
ServicesSection servicesSection = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
foreach (ServiceElement service in servicesSection.Services)
{
foreach (ServiceEndpointElement endpoint in service.Endpoints)
{
if (string.Compare(endpoint.Contract, contractName) == 0)
{
endpointAddresses.Add(new EndpointAddress(endpoint.Address));
}
}
}
if (endpointAddresses.Count == 0)
{
//TODO: Add logic to determine default
endpointAddresses.Add(new EndpointAddress("Your default here"));
}
return endpointAddresses;
}