C# 如何迭代WCF接口定义端点、操作和参数

C# 如何迭代WCF接口定义端点、操作和参数,c#,wcf,interface,definition,endpoint,C#,Wcf,Interface,Definition,Endpoint,如何以编程方式获得WCF接口定义端点、操作和参数,以便在代码中使用 这是我很久以来一直想知道的一个问题,最终在微软的MSDN网站上找到了我需要的解决方案。但愿我早点找到它。我将在下面提供答案和链接 使用System.ServiceModel.Description OperationDescription类 我在代码中得到了这样的ServiceHost Uri baseAddress = new Uri(OperationContext.Current.Host.BaseAddresses[0]

如何以编程方式获得WCF接口定义端点、操作和参数,以便在代码中使用

这是我很久以来一直想知道的一个问题,最终在微软的MSDN网站上找到了我需要的解决方案。但愿我早点找到它。我将在下面提供答案和链接

使用System.ServiceModel.Description OperationDescription类

我在代码中得到了这样的ServiceHost

Uri baseAddress = new Uri(OperationContext.Current.Host.BaseAddresses[0].AbsoluteUri);
ServiceHost sh = new ServiceHost(typeof(MyWebServiceClass), baseAddress);
下面的代码来自,完全符合我的需要

private void PrintDescription(ServiceHost sh)
{
    // Declare variables.
    int i, j, k, l, c;
    ServiceDescription servDesc = sh.Description;
    OperationDescription opDesc;
    ContractDescription contractDesc;
    MessageDescription methDesc;
    MessageBodyDescription mBodyDesc;
    MessagePartDescription partDesc;
    IServiceBehavior servBeh;
    ServiceEndpoint servEP;

    // Print the behaviors of the service.
    Console.WriteLine("Behaviors:");
    for (c = 0; c < servDesc.Behaviors.Count; c++)
    {
        servBeh = servDesc.Behaviors[c];
        Console.WriteLine("\t{0}", servBeh.ToString());                
    }

    // Print the endpoint descriptions of the service.
    Console.WriteLine("Endpoints");
    for (i = 0; i < servDesc.Endpoints.Count; i++)
    {
        // Print the endpoint names.
        servEP = servDesc.Endpoints[i];
        Console.WriteLine("\tName: {0}", servEP.Name);
        contractDesc = servEP.Contract;

        Console.WriteLine("\tOperations:");
        for (j = 0; j < contractDesc.Operations.Count; j++)
        {
            // Print the operation names.
            opDesc = servEP.Contract.Operations[j];
            Console.WriteLine("\t\t{0}", opDesc.Name);
            Console.WriteLine("\t\tActions:");
            for (k  = 0; k < opDesc.Messages.Count; k++)
            {
                // Print the message action. 
                methDesc = opDesc.Messages[k];
                Console.WriteLine("\t\t\tAction:{0}", methDesc.Action);

                // Check for the existence of a body, then the body description.
                mBodyDesc = methDesc.Body;
                if (mBodyDesc.Parts.Count > 0)
                {
                    for (l = 0; l < methDesc.Body.Parts.Count; l++)
                    {
                        partDesc = methDesc.Body.Parts[l];
                        Console.WriteLine("\t\t\t\t{0}",partDesc.Name);
                    }
                }
            }
        }
    }
}
private void打印描述(ServiceHost sh)
{
//声明变量。
int i,j,k,l,c;
ServiceDescription servDesc=sh.说明;
操作说明opDesc;
合同描述合同描述;
信息描述方法;
MessageBodyDescription mBodyDesc;
MessagePartDescription partDesc;
IServiceBehavior servBeh;
服务端点服务;
//打印服务的行为。
Console.WriteLine(“行为:”);
对于(c=0;c0)
{
对于(l=0;l
谢谢分享!请提供相关原始资料的实际链接。我很高兴。答案在主代码块上方。