c# - 将 wsdl 文件导入 C# WCF 项目,并公开 wsdl 合同
问题描述
我有一个第三方提供的 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)
我做了一个简单的demo,希望对你有用。
服务器:
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());
}
}
如果有什么我可以帮忙的,请随时告诉我。
推荐阅读
- java - 在java中创建一个计算句子、段落、字母和单词的程序
- java - 它甚至可以让我的 generated_keys 进入其他 SQL 吗?- “超过锁定等待超时;尝试重新启动事务”
- python - 解析用户输入以确定数学函数类型
- ios - 可点击的 UITableView 部分
- reactjs - Typescript+React+Redux+reactrouter:"property does not exist on type IntrinsicAttributes & IntrinsicClassAttributes" 将props从父传到子
- swift - 在 Swift 中通过 IF 语句设置变量值
- common-lisp - Ltk按钮配置
- azure - Azure AD 加入设备上的其他本地管理员不允许添加为本地管理员的用户
- java - 解码 Ldap 属性中的特殊字符
- javascript - 如何在打字稿中安全地声明嵌套对象类型