在 C# 中动态创建和公开 SOAP 服务及其 WSDL(使用自定义 TCP 侦听器!) [英] Creating and exposing a SOAP service and its WSDL dynamically in C# (with a custom TCP listener!)

查看:30
本文介绍了在 C# 中动态创建和公开 SOAP 服务及其 WSDL(使用自定义 TCP 侦听器!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C# 构建的自定义 HTTP 服务器,它接受对 REST 服务的请求并用 XML 或 JSON 响应(取决于客户端的需求).REST 服务是在运行时根据基于数据库的配置定义的,输入参数和输出类型差异很大,并且在生产环境中运行良好.

I've got a custom HTTP server built in C# which accepts requests for REST services and responds with XML or JSON (depending on what the client needs). The REST services are defined at runtime from a database-based configuration, vary widely in input parameters and output types, and it's working beautifully in production.

但是,我想添加对相同服务的 SOAP 访问,以及适当的 WSDL.由于可用服务不是硬编码的,这意味着:

However, I'd like to add SOAP access to the same services, with appropriate WSDLs as well. Since the available services aren't hard-coded, this means:

  • 发布从数据库中的方法定义在运行时生成的 WSDL
  • 解析传入的 SOAP 请求,将它们映射到那些定义,并确保请求在处理之前符合方法签名
  • 处理响应后,创建符合 WDSL 的 SOAP 响应以返回结果

MS 文档(和 Google)文档使用 Visual Studio 在设计时生成 Web 服务(和 WSDL),使用 WebMethods、ASP.NET MVC 等公开内容.这不是我要找的,因为那里没有在设计时生成绑定的方法定义.

The MS documentation (and Google) documents using Visual Studio to generate web services (and WSDLs) at design time, exposing stuff using WebMethods, ASP.NET MVC etc. This isn't what I'm looking for, as there are no method definitions from which to generate the bindings at design time.

有没有人有任何想法(例如用于原始 SOAP 解析的工具包),以及从动态创建的方法签名等生成 WSDL 的想法?如果没有,你知道如何建造这样的东西吗?如果可能,我希望避免重新发明轮子.

Does anyone have any ideas (e.g. toolkits for raw SOAP parsing), and thoughts on generation of WSDLs from dynamically created method signatures, etc? Any idea how one might go about building such things if not? I'm looking to avoid re-inventing the wheel if possible.

PS:很明显 .NET 框架中有标准化的东西,因为 Visual Studio 为你做了 - 任何想法如何在运行时在较低级别访问它?

PS: Clearly there's standardised stuff in the .NET framework for this, since Visual Studio does it for you - any ideas how to access that at a lower level, at runtime?

推荐答案

要动态创建 wsdl,您可以使用 ServiceDescriptionReflector

To create a wsdl dynamically you can use ServiceDescriptionReflector

例如:对于类

public class TestWebService
{
    [WebMethod]
    public string Hello(string namex)
    {
        return "Hello " + namex;
    }
}

您可以使用此代码

StringWriter wr = new StringWriter();
var r = new System.Web.Services.Description.ServiceDescriptionReflector();
r.Reflect(typeof(TestWebService), "http://somewhere.com");
r.ServiceDescriptions[0].Write(wr);
var wsdl = wr.ToString();

但是既然你说了

发布从数据库中的方法定义在运行时生成的 WSDL

Publishing a WSDL generated at runtime from the method definitions in the database

你必须在运行时创建Type

var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyAsm"), AssemblyBuilderAccess.Run);
var mod = asm.DefineDynamicModule("MyModule");

TypeBuilder typeBuilder = mod.DefineType("TestWebService");

MethodBuilder mb = typeBuilder.DefineMethod("Hello", MethodAttributes.Public, CallingConventions.Standard, typeof(string), new Type[] { typeof(string) });
var cab = new CustomAttributeBuilder( typeof(WebMethodAttribute).GetConstructor(new Type[]{}), new object[]{} );
mb.SetCustomAttribute(cab);
mb.DefineParameter(1, ParameterAttributes.In, "namex");
mb.GetILGenerator().Emit(OpCodes.Ret);

Type type = typeBuilder.CreateType();

现在你可以使用type来创建wsdl

Now you can use type to create wsdl

StringWriter wr = new StringWriter();
var r = new System.Web.Services.Description.ServiceDescriptionReflector();
r.Reflect(type, "http://somewhere.com");
r.ServiceDescriptions[0].Write(wr);
var wsdl = wr.ToString();

对于读取请求和形成响应,您可以使用Linq2Xml.Fiddler 可以让您了解客户端和服务器之间发送的 SOAP(xml) 格式

For reading request and forming response, you can use Linq2Xml. Fiddler can give you an idea about SOAP(xml) format sent between client and server

这篇关于在 C# 中动态创建和公开 SOAP 服务及其 WSDL(使用自定义 TCP 侦听器!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆