简体   繁体   English

在没有XSD的情况下共享WSDL类型

[英]Sharing WSDL types without XSD

I can't seem to find an example of generating proxies from WSDLs with shared types but without having any XSDs to go along with them. 我似乎无法找到从具有共享类型的WSDL生成代理但没有任何XSD与它们一起生成代理的示例。 Can anyone please mark this as duplicate and point me to an example please? 任何人都可以将此标记为重复,请指出一个例子吗?

Here are 2 services, each has its own namespace and a common type. 这里有2个服务,每个服务都有自己的命名空间和通用类型。 The only thing that is publicly accessible are their WSDLs, there is no type's XSD or its .dll to pass to wsdl.exe /sharedtypes or svcutils and without it I end up with identical class Foo that I can't pass in to SetFoo and class Foo1 . 唯一可公开访问的是它们的WSDL,没有类型的XSD或它的.dll传递给wsdl.exe /sharedtypessvcutils而没有它我最终得到了相同的class Foo ,我无法传递给SetFooclass Foo1

The best I could come up with is generating proxies programmatically and detecting duplicates via CodeDOM, ignoring DataContract / WebServiceBinding namespaces, but it's a huge mess... 我能想到的最好的方法是以编程方式生成代理并通过CodeDOM检测重复,忽略DataContract / WebServiceBinding命名空间,但这是一个巨大的混乱......

[WebService(Namespace = "http://tempuri.org/FOO1")]
public class Service1 : WebService
{
    [WebMethod]
    public Foo GetFoo()
    {
        return new Foo();
    }
}

[WebService(Namespace = "http://tempuri.org/FOO2")]
public class Service2 : WebService
{
    [WebMethod]
    public void SetFoo(Foo foo)
    {
    }
}

public class Foo
{
    public int Bar { get; set; }
}

There is a way of doing this, which is outlined here . 有这样做,这是概括的方式在这里

In your case you can skip the first step, generate the proxy from service 1 and then use the /r flag on svcutil to reference the service 1 proxy assembly when you generate your service 2 proxy. 在您的情况下,您可以跳过第一步,从服务1生成代理,然后在生成服务2代理时使用svcutil上的/ r标志引用服务1代理程序集。

This will ensure your service 2 proxy will use the same instance of Foo from your service 1 proxy. 这将确保您的服务2代理将使用来自您的服务1代理的相同Foo实例。

However, have you considered just hosting a single service with two operations? 但是,您是否考虑过只使用两个操作托管单个服务? It would save you a lot of work. 它会为你节省很多工作。

Edit: Also have a look at this post: http://blogs.msdn.com/b/youssefm/archive/2009/10/09/reusing-types-in-referenced-assemblies-with-svcutil-sr-switch.aspx 编辑:另请看这篇文章: http//blogs.msdn.com/b/youssefm/archive/2009/10/09/reusing-types-in-referenced-assemblies-with-svcutil-sr-switch。 ASPX

First off, you need to set the [DataContract(Namespace="some namespace here")] for all common service data types, otherwise when the WSDL and XSDs are generated then you will have objects from two difference namespace --- this is absolutely essential . 首先,你需要为所有常见的服务数据类型设置[DataContract(Namespace =“some namespace”)],否则当生成WSDL和XSD时,你将拥有来自两个不同命名空间的对象---这绝对是必不可少 The namespace value will only apply to the types defined in the XSD and not in the WSDL. 命名空间值仅适用于XSD中定义的类型,而不适用于WSDL中。 XSD = data, WSDL = service. XSD =数据,WSDL =服务。

The XSDs and WSDL and generated if, and only if, you have the META service behavior set - add this behavior and then you can navigate to the URL. XSD和WSDL是在且仅当您具有META服务行为集时生成的 - 添加此行为,然后您可以导航到URL。 The URL of the META service behavior will then have a link to your WSDLs and XSDs. 然后,META服务行为的URL将具有指向WSDL和XSD的链接。

I use the following piece of code to self-host services in windows services rather than through IIS, however the same principals apply.... 我使用下面的代码片段在Windows服务中而不是通过IIS自托管服务,但是同样的原则适用于....

/// <summary>
/// Enables meta data output for a service host.
/// </summary>
/// <param name="host">The service host.</param>
/// <remarks>Must be invoked prior to starting the service host.</remarks>
public static void SetupMetaDataBehaviour(ServiceHost host)
{
    ServiceMetadataBehavior metaDataBehaviour = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
    if (metaDataBehaviour == null)
    {
        metaDataBehaviour = new ServiceMetadataBehavior();
        metaDataBehaviour.HttpGetEnabled = true;
        host.Description.Behaviors.Add(metaDataBehaviour);
    }
    else
    {
        metaDataBehaviour.HttpGetEnabled = true;
    }
}

after adding your two web references: 添加两个Web引用后:

  1. double click on the second web service reference 双击第二个Web服务引用
  2. in the object browser navigate to the definition of Foo 在对象浏览器中导航到Foo的定义
  3. right click on Foo and choose go to definition 右键单击Foo并选择转到定义
  4. delete the definition for the class Foo 删除类Foo的定义
  5. add a using statement for the namespace of webservice one 为webservice的名称空间添加一个using语句
  6. find and replace all instances of <namespace-of-service-reference-2>.Foo with just Foo 使用Foo查找并替换<namespace-of-service-reference-2>.Foo所有实例

This should fix your problem as it forces the autogenerated code for both service references to use the same class declaration. 这应该可以解决您的问题,因为它会强制两个服务引用的自动生成的代码使用相同的类声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM