从 EndpointAddress 创建 WCF 绑定的简单方法

发布于 2024-10-16 07:03:01 字数 375 浏览 1 评论 0原文

是否有基于给定端点的地址创建最基本的 WCF 绑定的快捷方式?

端点:net.tcp://localhost:7879/Service.svc

而不是一大块 if 语句...

Binding binding = null;

if (endpoint.StartsWith("net.tcp"))
{
    binding = new NetTcpBinding();
}
else if (endpoint.StartWith("http"))
{
    binding = new WsHttpBinding();
}

.
.
.

框架库中是否有一个快捷方式可以为我执行此操作,但我找不到或者我可以找不到它,因为它不公开存在?

Is there a shortcut for creating the most basic WCF Binding based on the address of a given Endpoint?

Endpoint: net.tcp://localhost:7879/Service.svc

Instead of a big block of if statements...

Binding binding = null;

if (endpoint.StartsWith("net.tcp"))
{
    binding = new NetTcpBinding();
}
else if (endpoint.StartWith("http"))
{
    binding = new WsHttpBinding();
}

.
.
.

Is there a shortcut in the Framework library that will do this for me that I just can't find or can I not find it because it doesn't publicly exist?

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

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

发布评论

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

评论(3

故事与诗 2024-10-23 07:03:01

.NET 4 中的 WCF 自动为您执行此操作 - 该功能称为“默认端点”。

请在此处了解 WCF 4 的所有新功能:WCF 4 开发人员简介

默认端点大约是文章的第二段左右。

WCF in .NET 4 does that automatically for you - the feature is called default endpoints.

Read about all of WCF 4's new features here: A Developer's Introduction to WCF 4

Default endpoints is about the second or so paragraph into the article.

厌倦 2024-10-23 07:03:01

虽然 WCF 4 支持默认服务端点,但它不支持默认客户端端点。不幸的是,框架用于创建默认绑定的方法是内部的,但其背后的逻辑很简单,因此我重新实现了它以在客户端使用(跳过原始缓存和跟踪逻辑):

private static Binding GetBinding(string scheme)
{
    // replace with ConfigurationManager if not running in ASP.NET
    var configuration = WebConfigurationManager.OpenWebConfiguration(null);
    var sectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
    Debug.Assert(sectionGroup != null, "system.serviceModel configuration section is missing.");
    var mapping = sectionGroup.ProtocolMapping.ProtocolMappingCollection
                              .OfType<ProtocolMappingElement>()
                              .SingleOrDefault(e => e.Scheme == scheme);
    if (mapping == null)
        throw new NotSupportedException(string.Format("The URI scheme {0} is not supported.", scheme));
    var bindingElement = sectionGroup.Bindings.BindingCollections.Single(e => e.BindingName == mapping.Binding);
    var binding = (Binding) Activator.CreateInstance(bindingElement.BindingType);
    var bindingConfiguration = bindingElement.ConfiguredBindings.SingleOrDefault(e => e.Name == mapping.BindingConfiguration);
    if (bindingConfiguration != null) 
        bindingConfiguration.ApplyConfiguration(binding);
    return binding;
}

没有任何配置,此代码相当于问题中的代码,但您可以在 system.serviceModel/protocolMapping 部分中选择和配置绑定。

While WCF 4 supports default service endpoints, it does not support default client endpoints. Unfortunately the methods used by the framework to create default bindings are internal, but the logic behind it is simple, so I have reimplemented it to use on the client side (skipping original caching and tracing logic):

private static Binding GetBinding(string scheme)
{
    // replace with ConfigurationManager if not running in ASP.NET
    var configuration = WebConfigurationManager.OpenWebConfiguration(null);
    var sectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
    Debug.Assert(sectionGroup != null, "system.serviceModel configuration section is missing.");
    var mapping = sectionGroup.ProtocolMapping.ProtocolMappingCollection
                              .OfType<ProtocolMappingElement>()
                              .SingleOrDefault(e => e.Scheme == scheme);
    if (mapping == null)
        throw new NotSupportedException(string.Format("The URI scheme {0} is not supported.", scheme));
    var bindingElement = sectionGroup.Bindings.BindingCollections.Single(e => e.BindingName == mapping.Binding);
    var binding = (Binding) Activator.CreateInstance(bindingElement.BindingType);
    var bindingConfiguration = bindingElement.ConfiguredBindings.SingleOrDefault(e => e.Name == mapping.BindingConfiguration);
    if (bindingConfiguration != null) 
        bindingConfiguration.ApplyConfiguration(binding);
    return binding;
}

Without any configuration this code is equivalent to the code in the question, but you can select and configure your bindings inside the system.serviceModel/protocolMapping section.

违心° 2024-10-23 07:03:01

更深入地研究问题后,我真的不需要手动读取配置。相反,我需要发送绑定信息以及地址和合同。

http://www.codeproject.com/KB/WCF/WCFDiscovery.aspx ?display=PrintAll

我构建了一个简单的组件来序列化绑定信息。

http://nardax.codeplex.com/

After looking at the issue deeper I don't really need to read the configuration in manually. Instead I need to send the binding information along with the address and contract.

http://www.codeproject.com/KB/WCF/WCFDiscovery.aspx?display=PrintAll

I have built a simple component that serializes the binding information.

http://nardax.codeplex.com/

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