ServiceStack:VS 2012 添加服务引用

ServiceStack: VS 2012 Add service reference

我在向我的 soap 端点添加服务引用时遇到问题。我什至尝试在 SS 网站 http://mono.servicestack.net/soap11 上添加 hello 示例的地址,但无法生成 wsdl。我所有的 dto(在我的项目中)都装饰有数据合同/成员。我还更改了程序集以指向目标命名空间。我试过将它添加为 Web 引用,并在引用程序集中标记重用类型关闭,但仍然没有成功。我有什么忘记做的吗?如果需要更多信息,请告诉我。

using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WILP_API
{
    public class ApplicationHost : AppHostBase
    {
        public ApplicationHost() : base("GreetingService", typeof(GreetingService).Assembly) { }

        public override void Configure(Funq.Container container)
        {
            //throw new NotImplementedException();
        }
    }
}​
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace WILP_API
{
    [Route("/hello/{Name}","GET")]
    [DataContract(Namespace="WILP_API")]
    public class GreetingRequest
    {
        [DataMember]
        public string Name { get; set; }
    }
}
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace WILP_API
{
    [Route("/hello/{Name}", "GET")]
    [DataContract(Namespace="WILP_API")]
    public class GreetingResponse
    {
        [DataMember]
        public string Result { get; set; }
    }
}​
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WILP_API
{
    public class GreetingService : IService
    {
        public GreetingResponse Any(GreetingRequest request)
        {
            GreetingResponse response = new GreetingResponse();
            response.Result = "Hello, " + request.Name + "!";
            return response;
        }
    }
}​
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WILP_API")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WILP_API")]
[assembly: AssemblyCopyright("Copyright ©  2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("133bdb3e-442d-45ad-9cc2-02fbcd50c8ac")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Revision and Build Numbers 
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ContractNamespace("http://schemas.servicestack.net/types",
           ClrNamespace = "WILP_API")]
[assembly: ContractNamespace("http://schemas.servicestack.net/types", ClrNamespace = "ServiceStack")]
[assembly: ContractNamespace("http://schemas.servicestack.net/types", ClrNamespace = "ServiceStack.Client")]​

错误:

Error 5 Custom tool error: Failed to generate code for the service reference 'ServiceReference3'. Please check other error and warning messages for details.

Warning 3 Custom tool warning: Cannot import wsdl:binding Detail: There was an error importing a wsdl:portType that the wsdl:binding is dependent on. XPath to wsdl:portType: //wsdl:definitions[@targetNamespace='http://schemas.servicestack.net/types']/wsdl:portType[@name='ISyncReply'] XPath to Error Source: //wsdl:definitions[@targetNamespace='http://schemas.servicestack.net/types']/wsdl:binding[@name='BasicHttpBinding_ISyncReply']

Warning 4 Custom tool warning: Cannot import wsdl:port Detail: There was an error importing a wsdl:binding that the wsdl:port is dependent on. XPath to wsdl:binding: //wsdl:definitions[@targetNamespace='http://schemas.servicestack.net/types']/wsdl:binding[@name='BasicHttpBinding_ISyncReply'] XPath to Error Source: //wsdl:definitions[@targetNamespace='http://schemas.servicestack.net/types']/wsdl:service[@name='SyncReply']/wsdl:port[@name='BasicHttpBinding_ISyncReply']

Warning 2 Custom tool warning: Cannot import wsdl:portType Detail: An exception was thrown while running a WSDL import extension: System.ServiceModel.Description.DataContractSerializerMessageContractImporter Error: There was a problem loading the XSD documents provided: a reference to a schema element with name 'GreetingRequest' and namespace 'http://schemas.servicestack.net/types' could not be resolved because the element definition could not be found in the schema for targetNamespace 'http://schemas.servicestack.net/types'. Please check the XSD documents provided and try again. XPath to Error Source: //wsdl:definitions[@targetNamespace='http://schemas.servicestack.net/types']/wsdl:portType[@name='ISyncReply']

您应该确保只有 single WSDL Namespace 用于所有 DTO。由于 ServiceStack 内置 DTO 已经用 http://schemas.servicestack.net/types 定义,因此最简单并建议您坚持通过在 Assembly.cs 中添加程序集 [ContractNamespace] 属性来完成的操作,例如:

[assembly: ContractNamespace("http://schemas.servicestack.net/types",
           ClrNamespace = "WILP_API")]

这将适用于 WILP_API 命名空间下该程序集中的所有 DTO,因此它们将不再需要在 DTO 上定义命名空间,例如:

[Route("/hello/{Name}","GET")]
[DataContract]
public class Greeting : IReturn<GreetingResponse>
{
    [DataMember]
    public string Name { get; set; }
}

[DataContract]
public class GreetingResponse
{
    [DataMember]
    public string Result { get; set; }
}

也只有 Request DTO 使用 [Route] 属性。您还应该遵循 Greeting/GreetingResponse Request/Response DTO Naming Convention.

更改 WSDL 命名空间

虽然建议坚持使用默认的 ServiceStack 命名空间,但如果您必须更改它,您还需要在 HostConfig 中配置不同的命名空间,例如:

SetConfig(new HostConfig {
    WsdlServiceNamespace = "http://new.namespace.com",
});

Add ServiceStack Reference

您应该考虑使用 ServiceStack's new Add ServiceStack Reference which offers numerous advantages over WCF's SOAP Add Service Reference.

而不是使用 SOAP 端点和 WSDL

我最近正好遇到了这个问题,我想专门扩展一下上面的答案。在我的案例中,结果 100% 与使用的命名约定有关。

推荐的命名约定 here 在 SS 生成 wsdl 时在某种程度上得到了强制执行。我不确定它是有意还是错误,但以 'Request' 结束请求 DTO(至少截至 2017 年 1 月)会导致这三个警告和在 VS 中添加标准 .Net 服务引用时失败。