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

发布于 2024-10-31 13:46:32 字数 140 浏览 3 评论 0原文

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

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

In a self-hosted service, I'd like to use the endpoint(s) specified in App.config, if present, or a default endpoint specified in code if App.config is empty. How can I do this?

Edit: to clarify, this is on the server (service) side using ServiceHost.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不奢求什么 2024-11-07 13:46:32

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

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

One way is to try the first attempt to load from the config file, and hard-code the endpoint in the catch. EG:

MyServiceClient client = null;
try
{
    client = new MyServiceClient();
}
catch (InvalidOperationException)
{
    EndpointAddress defaultAddress = new EndpointAddress(...);
    Binding defaultBinding = new Binding(...);
    client = new MyServiceClient(defaultBinding, defaultAddress);
}
南风起 2024-11-07 13:46:32

您可以按如下方式获取配置节:

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

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

You can get the configuration section as below:

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

If value is null or if clientSection.Endpoints contains zero elements then it is not defined.

绝不服输 2024-11-07 13:46:32

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

I have faced to same kind of issue when I wanted to implement the standalone service client without app.config file. and Finally I could sort it out. Please follow the below code sample. It is working fine and I have tested it.

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();
心碎无痕… 2024-11-07 13:46:32

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

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

Try this...untested, but should work for you. It will check your configuration for any endpoints with a matching contract. You can change it to match by name, return different information, or whatever makes sense for your situation. If no matches is found, you can put logic in to create a default endpoint.

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文