c# - 以编程方式让客户端应用程序使用 URL 列出在运行时创建的 WCF 服务端点(如 WCF 测试客户端所做的那样)

标签 c# wcf

也许我没有使用正确的术语进行搜索,因为这看起来很简单。我只是在寻找一种方法来列出 WCF 服务的端点,该服务的端点在运行时创建,就像 WCF 测试客户端那样。

  1. 指定网址

Add URL

  • 获取元数据和端点
  • enter image description here

    这是我在运行时添加端点的方法

    string SetInstrumentsURL = serviceUrl + "SetInstruments/";
    string SetInstrumentsPipe = "net.pipe://localhost/TestService/SetInstruments/";
    ServiceHost SetInstrumentsHost = null;
    var SetInstruments = InstrumentLoader.Factory.GetIEnumerableOf<ISetInstrument>();
    if (SetInstruments.Count() > 0)
    {
        Uri SetInstrumentsURI = new Uri(SetInstrumentsURL);
        Uri SetInstrumentsPipedURI = new Uri(SetInstrumentsPipe);
        NetTcpBinding netTcpBindingSetInstruments = new NetTcpBinding();
        NetNamedPipeBinding NamedPipeBindingSetInstruments = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
        SetInstrumentsHost = new ServiceHost(typeof(TeraSetInstrumentService), new Uri[] { SetInstrumentsURI, SetInstrumentsPipedURI });
        ServiceMetadataBehavior SetInstrumentServiceMetadataBehavior = new ServiceMetadataBehavior();
        SetInstrumentsHost.Description.Behaviors.Add(SetInstrumentServiceMetadataBehavior);
        SetInstrumentsHost.AddServiceEndpoint(typeof(IMetadataExchange),
        MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
        SetInstrumentsHost.AddServiceEndpoint(typeof(IMetadataExchange),
        MetadataExchangeBindings.CreateMexNamedPipeBinding(), "mex");
        foreach (var setter in SetInstruments)
        {
            SetInstrumentsHost.AddServiceEndpoint(typeof(ISetInstrumentService), netTcpBindingSetInstruments, SetInstrumentsURL + setter.Name).Name = "Set_" + setter.Name.Replace(" ", "_");
            SetInstrumentsHost.AddServiceEndpoint(typeof(ISetInstrumentService), NamedPipeBindingSetInstruments, SetInstrumentsPipe + setter.Name).Name = "Set_" + setter.Name.Replace(" ", "_");
        }
        SetInstrumentsHost.Open();
    }
    

    我可以从客户端使用哪些函数来访问与 WCF 测试客户端相同的端点?如果我已经有了端点的 URL,我知道如何连接到这些端点,但我想要一个端点列表,以便我可以创建一个下拉列表,根据您连接到的主机从该更改中进行选择。

    通过 Visual Studio 添加服务引用不会列出所有端点,因为尚未创建端点。我可以使用这个库在运行时获取它们,就像 WCF 测试客户端那样。

    最佳答案

    如果我们有服务元数据 URI,我们可以使用 System.ServiceModel.Description 命名空间中提供的 MetadataExchangeClientMode 和 MetadataResolver 类来检索和处理元数据。
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-metadataresolver-to-obtain-binding-metadata-dynamically
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-metadataexchangeclient-to-retrieve-metadata

    我做了一个简单的例子,希望对你有用。

        class Program
        {
            static void Main(string[] args)
            {
                Uri uri = new Uri("http://10.157.13.69:3336/mex");
                MetadataExchangeClient client = new MetadataExchangeClient(uri, MetadataExchangeClientMode.MetadataExchange);
                MetadataSet metadata = client.GetMetadata();
                WsdlImporter importer = new WsdlImporter(metadata);
                ServiceEndpointCollection endpoints = importer.ImportAllEndpoints();
    
                //ServiceEndpointCollection endpoints = MetadataResolver.Resolve(typeof(IService), uri, MetadataExchangeClientMode.MetadataExchange);
                foreach (var item in endpoints)
                {
                    Console.WriteLine(item.Address.Uri);
                }
            }
        }
        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            string SayHello();
    }
    

    结果
    enter image description here

    关于c# - 以编程方式让客户端应用程序使用 URL 列出在运行时创建的 WCF 服务端点(如 WCF 测试客户端所做的那样),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657280/

    相关文章:

    c# - 从 Rad Month Year Picker 获取准确值

    c# - 通过 linq 选择组合组的前两个

    WCF + CQRS + 推送通知

    silverlight - 如何修复 HTTPS Silverlight 应用程序上下文中的 WCF maxClockSkew 问题?

    c# - 如何使用 Autofac 将 WCF 服务实现注入(inject)到另一个 WCF 服务中

    c# - Azure:表脚本不是由 sql 执行触发的

    c# - 根据 AD 组检查用户登录

    android - Xamarin Android - 使用复杂参数(对象)发出 Rest 请求会引发异常,在 .NET 中它工作正常(使用 channel 工厂)

    c# - XNode 到没有命名空间的子元素的字符串

    c# - 使用soap客户端时出现InvalidOperationException