[英]Import wsdl file into C# WCF project, and expose wsdl contracts
我有一個由第三方提供的wsdl文件,我需要按原樣使用它並公開該wsdl中的合同以供使用。
我的問題是我的項目有自己的命名空間,而wsdl帶有不同的命名空間,我不知道如何完成工作。
感謝任何幫助
編輯
第三方(gov)希望使用其名稱空間來調用服務
示例:我有一個帶有名稱空間的WCF服務應用程序 : local.namespace
WSDL:
<wsdl:definitions xmlns:ns0="http://com.gov.update.ws" targetNamespace="http://com.gov.update.ws">
<wsdl:message name="updateStatus">
<wsdl:part name="parameters" element="xsns:updateStatus" xmlns:xsns="http://com.gov.update.ws"/>
</wsdl:message>
</wsdl:definitions>
收到的SOAP:
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header>
<ctx:clientContext xmlns:ctx="http://ClientContext">
<ctx:clientUserId>123456</ctx:clientUserId>
</ctx:clientContext>
</soapenv:Header>
<soapenv:Body>
<p820:updateStatus xmlns:p820="http://com.gov.update.ws">
<transactionId>123456</transactionId>
<status>Accepted</status>
</p820:updateStatus>
</soapenv:Body>
</soapenv:Envelope>
通常,使用客戶端代理類通過添加服務引用來調用Web服務是相對常見的。 如下。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
您也可以通過SVCUtil工具生成客戶端代理類。
https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/aa347733(v=vs.90)
我做了一個簡單的演示,希望對您有用。
服務器:
namespace Server8
{
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://localhost:1900");
BasicHttpBinding binding = new BasicHttpBinding();
using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
{
sh.AddServiceEndpoint(typeof(IService), binding, "");
ServiceMetadataBehavior smb;
smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb==null)
{
smb = new ServiceMetadataBehavior()
{
HttpGetEnabled = true
};
sh.Description.Behaviors.Add(smb);
}
Binding binding1 = MetadataExchangeBindings.CreateMexHttpBinding();
sh.AddServiceEndpoint(typeof(IMetadataExchange), binding1, "mex");
sh.Open();
Console.WriteLine("Service is ready...");
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract(Namespace ="mydomain")]
public interface IService
{
[OperationContract(Name ="AddInt")]
int Add(int x, int y);
}
public class MyService : IService
{
public int Add(int x, int y)
{
return x + y;
}
}
}
WSDL地址是
Http://localhost:1900?wsdl
Svctutil工具。
Svcutil http://localhost:1900?wsdl /directory:D: /namespace:”mydomain”,”LocalProjectNamespace”
該命令將在本地D分區中生成一個客戶端代理類,並將Web服務的名稱空間替換為“ LocalProjectNamespace”,它還會生成一個客戶端配置文件(xml),該文件描述了服務的綁定和端點信息。
然后,我們通過客戶端代理類調用Web服務。
static void Main(string[] args)
{
ServiceClient client = new ServiceClient();
try
{
var result = client.AddInt(23, 55);
Console.WriteLine(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
請隨時告訴我是否有什么我可以幫助的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.