[英]How to Generate WCF Service HOST from Existing WSDL & XSD Files
我有来自我们的服务器之一的现有 WSDL 和 XSD 文件,该服务器使用 WAS(Windows 激活服务)运行 IIS 8 和 ASP.NET 4.5。 我正在为该项目使用 VS 2012。 我没有 WCF 服务主机的原始代码,所以我需要从现有的 WSDL 和 XSD 文件创建 WCF 主机。
我已经有了客户端代理代码,即客户端已完成。
我需要从现有的 WSDL 和 XSD 文件生成将托管在 IIS 8 服务器中的 WCF 服务主机吗? 不是客户端代理代码 - 我已经完成了。
我有以下文件
注意:MyMainService.wsdl 指向包含调用 myFirstXSD.xsd 和 mySecondXSD.xsd 的代码的 mySubService.wsdl 我也有单个 WSDL,即 myServiceSingleWSDL.wsdl
我通读了如何使用 SvcUtil.exe 和 Disco.exe,但我不确定如何正确使用。 大多数相似的问题都没有回答问题,而只是绕过它。
如果您可以包含用于执行此操作的命令,请提供分步说明。 任何帮助将不胜感激。
因此,我运行以下命令以从上述文件 SvcUtil /mc /language:C# /out:IService1.cs /n:*,Contoso.WcfServiceHost MyMainService.wsdl MySubService.wsdl MyFirstXSD.xsd MySecondXSD.xsd 生成以下代码
从这里开始下一步是什么? 生成将托管在 IIS 8 中的 WCF 服务主机,以便客户端可以连接到它或使用它
这里生成的 Web.config 文件
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IMyService">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://contoso.com/MyService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMyService"
contract="IMyService" name="WSHttpBinding_IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"></serviceHostingEnvironment>
</system.serviceModel>
</configuration>
下面是生成的 Iservice1.cs 文件
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18051
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Contoso.WcfServiceHost
{
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://contoso.com/MyService.svc", ConfigurationName = "IMyService")]
public interface IMyService
{
[System.ServiceModel.OperationContractAttribute(Action = "http://contoso.com/MyService.svc/IMyService/Login", ReplyAction = "http://contoso.com/MyService.svc/IMyService/LoginResponse")]
LoginResponse Login(LoginRequest request);
[System.ServiceModel.OperationContractAttribute(Action = "http://contoso.com/MyService.svc/IMyService/Login", ReplyAction = "http://contoso.com/MyService.svc/IMyService/LoginResponse")]
System.Threading.Tasks.Task<LoginResponse> LoginAsync(LoginRequest request);
[System.ServiceModel.OperationContractAttribute(Action = "http://contoso.com/MyService.svc/IMyService/Logout", ReplyAction = "http://contoso.com/MyService.svc/IMyService/LogoutResponse")]
LogoutResponse Logout(LogoutRequest request);
[System.ServiceModel.OperationContractAttribute(Action = "http://contoso.com/MyService.svc/IMyService/Logout", ReplyAction = "http://contoso.com/MyService.svc/IMyService/LogoutResponse")]
System.Threading.Tasks.Task<LogoutResponse> LogoutAsync(LogoutRequest request);
[System.ServiceModel.OperationContractAttribute(Action = "http://contoso.com/MyService.svc/IMyService/GetId", ReplyAction = "http://contoso.com/MyService.svc/IMyService/GetIdResponse")]
GetIdResponse GetId(GetIdRequest request);
[System.ServiceModel.OperationContractAttribute(Action = "http://contoso.com/MyService.svc/IMyService/GetId", ReplyAction = "http://contoso.com/MyService.svc/IMyService/GetIdResponse")]
System.Threading.Tasks.Task<GetIdResponse> GetIdAsync(GetIdRequest request);
}
[System.ServiceModel.MessageContractAttribute(WrapperName = "Login", WrapperNamespace = "http://contoso.com/MyService.svc", IsWrapped = true)]
public partial class LoginRequest
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://contoso.com/MyService.svc", Order = 0)]
public string login;
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://contoso.com/MyService.svc", Order = 1)]
public string password;
public LoginRequest()
{
}
public LoginRequest(string login, string password)
{
this.login = login;
this.password = password;
}
}
[System.ServiceModel.MessageContractAttribute(WrapperName = "LoginResponse", WrapperNamespace = "http://contoso.com/MyService.svc", IsWrapped = true)]
public partial class LoginResponse
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://contoso.com/MyService.svc", Order = 0)]
public System.Guid LoginResult;
public LoginResponse()
{
}
public LoginResponse(System.Guid LoginResult)
{
this.LoginResult = LoginResult;
}
}
[System.ServiceModel.MessageContractAttribute(WrapperName = "Logout", WrapperNamespace = "http://contoso.com/MyService.svc", IsWrapped = true)]
public partial class LogoutRequest
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://contoso.com/MyService.svc", Order = 0)]
public System.Guid sessionId;
public LogoutRequest()
{
}
public LogoutRequest(System.Guid sessionId)
{
this.sessionId = sessionId;
}
}
[System.ServiceModel.MessageContractAttribute(WrapperName = "LogoutResponse", WrapperNamespace = "http://contoso.com/MyService.svc", IsWrapped = true)]
public partial class LogoutResponse
{
public LogoutResponse()
{
}
}
[System.ServiceModel.MessageContractAttribute(WrapperName = "GetId", WrapperNamespace = "http://contoso.com/MyService.svc", IsWrapped = true)]
public partial class GetIdRequest
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://contoso.com/MyService.svc", Order = 0)]
public System.Guid sessionId;
public GetIdRequest()
{
}
public GetIdRequest(System.Guid sessionId)
{
this.sessionId = sessionId;
}
}
[System.ServiceModel.MessageContractAttribute(WrapperName = "GetIdResponse", WrapperNamespace = "http://contoso.com/MyService.svc", IsWrapped = true)]
public partial class GetIdResponse
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://contoso.com/MyService.svc", Order = 0)]
public long GetIdResult;
public GetIdResponse()
{
}
public GetIdResponse(long GetIdResult)
{
this.GetIdResult = GetIdResult;
}
}
public interface IMyServiceChannel : IMyService, System.ServiceModel.IClientChannel
{
}
public partial class MyServiceClient : System.ServiceModel.ClientBase<IMyService>, IMyService
{
public MyServiceClient()
{
}
public MyServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public MyServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public MyServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public MyServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public LoginResponse Login(LoginRequest request)
{
return base.Channel.Login(request);
}
public System.Threading.Tasks.Task<LoginResponse> LoginAsync(LoginRequest request)
{
return base.Channel.LoginAsync(request);
}
public LogoutResponse Logout(LogoutRequest request)
{
return base.Channel.Logout(request);
}
public System.Threading.Tasks.Task<LogoutResponse> LogoutAsync(LogoutRequest request)
{
return base.Channel.LogoutAsync(request);
}
public GetIdResponse GetId(GetIdRequest request)
{
return base.Channel.GetId(request);
}
public System.Threading.Tasks.Task<GetIdResponse> GetIdAsync(GetIdRequest request)
{
return base.Channel.GetIdAsync(request);
}
}
}
svcutil.exe 不会为您生成服务代码(听起来您已经发现了)。
如果原始服务由您/您的公司创建或拥有,或者您拥有源代码的其他合法权利,您可以使用Reflector 、 JustDeCompile或类似工具(Reflector 有免费试用但您必须为长期使用付费,JustDecompile 是免费但不如 Reflector 灵活)来对 DLL/EXE 进行反向工程(假设您可以访问 DLL/EXE)。
如果这不是您的选择,您可以研究合同优先开发,尽管我自己从未使用过它:
使用 Windows Communication Foundation 进行基于架构的开发
我还看到了对 Thinktecture 的 WSCF.blue 的引用,这可能值得一看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.