0
如何通過編程在程序中使用WCF接口定義的端點,操作和參數?如何通過WCF接口定義端點,操作和參數進行迭代
如何通過編程在程序中使用WCF接口定義的端點,操作和參數?如何通過WCF接口定義端點,操作和參數進行迭代
這是一個我想了解很長時間的問題,終於在微軟的MSDN網站上找到了我需要的解決方案。只希望我早點找到它。我將在下面提供答案和鏈接。
使用System.ServiceModel.Description OperationDescription類。
我在我的代碼中得到了我的ServiceHost。
Uri baseAddress = new Uri(OperationContext.Current.Host.BaseAddresses[0].AbsoluteUri);
ServiceHost sh = new ServiceHost(typeof(MyWebServiceClass), baseAddress);
以下代碼來自Microsoft MSDN documentation,完全符合我的需求。
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);
}
}
}
}
}
}
感謝分享!請提供與相關原始資料的實際鏈接。 –
我的榮幸。它位於主代碼塊上方的答案中。 –