[英]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.