Web Services 如何从现有WSDL和XSD文件生成WCF服务HOST

v2g6jxz6  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(156)

我有来自我们的服务器之一的现有WSDL和XSD文件,该服务器运行IIS 8和ASP.NET 4.5,使用WAS(Windows激活服务)。我使用的是项目的VS 2012。我没有WCF服务主机的原始代码,因此我需要从现有WSDL和XSD文件创建WCF主机。
我有客户端代理代码已经即客户端是部分完成。
我需要从现有的WSDL和XSD文件生成将在IIS 8服务器中托管的WCF服务主机-不是客户端代理代码-我已经完成了。
我有以下文件

  1. myMainService.wsdl
  2. mySubService.wsdl
  3. myFirstXSD.xsd
  4. mySecondXSD.xsd
  5. myServiceSingleWSDL.wsdl
    1.客户端代理代码(完成)
    1.测试客户端(完成)
    注意:MyMainService.wsdl指向mySubService.wsdl,它包含调用myFirstXSD.xsd和mySecondXSD.xsd的代码。我还有一个WSDL,即myServiceSingleWSDL.wsdl。
    我通读了如何使用SvcUtil.exe和Disco.exe,但我不确定如何正确使用。大多数类似的问题都没有回答这个问题,只是绕了一圈。
    请给予一个步骤,如果你可以包括命令来做这件事。任何帮助将不胜感激。
    因此,我运行以下命令,从上述文件SvcUtil /mc /language中生成以下代码:C# /out:IService1.cs /n:*,Contoso.WcfServiceHost我的主服务.wsdl我的子服务.wsdl我的第一个XSD. xsd我的第二个XSD.xsd
    下一步是什么?生成将在IIS 8中托管的WCF服务HOST,以便客户端可以连接到它或使用它
    下面是生成的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);
                    }
            }

        }
hs1ihplo

hs1ihplo1#

svcutil.exe不会为您生成服务代码(听起来您已经知道了)。
如果原始服务是由您/您的公司创建或拥有的,或者您对源代码拥有法律的权利,您可以使用ReflectorJustDeCompile或类似工具(Reflector有免费试用版,但长期使用需要付费,JustDecompile是免费的,但不如Reflector灵活)对DLL/EXE进行反向工程(假设您可以访问DLL/EXE)。
如果您没有选择,您可以考虑使用Contract-First开发,尽管我自己从未使用过它:
Schema-based Development with Windows Communication Foundation
我还看到了Thinktecture的WSCF.blue的参考资料,这可能值得一看。

相关问题