如何对服务的默认WCF端点进行硬编码?

时间:2011-04-07 16:21:45

标签: .net wcf endpoint wcf-endpoint

在自托管服务中,我想使用App.config中指定的端点(如果存在),或者如果App.config为空,则使用代码中指定的默认端点。我怎么能这样做?

编辑:澄清一下,这是在使用ServiceHost的服务器(服务)端。

4 个答案:

答案 0 :(得分:2)

一种方法是try首次尝试从配置文件加载,并在catch中对端点进行硬编码。 EG:

MyServiceClient client = null;
try
{
    client = new MyServiceClient();
}
catch (InvalidOperationException)
{
    EndpointAddress defaultAddress = new EndpointAddress(...);
    Binding defaultBinding = new Binding(...);
    client = new MyServiceClient(defaultBinding, defaultAddress);
}

答案 1 :(得分:2)

您可以按以下方式获取配置部分:

var clientSection =  System.Configuration.ConfigurationManager.GetSection("system.serviceModel/client");

如果value为null或clientSection.Endpoints包含零元素,则表示未定义。

答案 2 :(得分:2)

当我想要在没有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();

答案 3 :(得分:1)

试试这个......未经测试,但应该适合你。它将检查具有匹配合同的任何端点的配置。您可以将其更改为按名称匹配,返回不同的信息或任何对您的情况有意义的信息。如果未找到匹配项,则可以将逻辑放入以创建默认端点。

    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;
    }