创建和使用C#动态露出一个SOAP服务及其WSDL(使用自定义的TCP监听!) [英] Creating and exposing a SOAP service and its WSDL dynamically in C# (with a custom TCP listener!)

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

问题描述

我有建在C#中的自定义HTTP服务器并同意对REST服务请求和响应XML或JSON(这取决于客户的需求)。其余的服务从基于数据库的配置运行时定义的,有很大的不同的输入参数和输出类型,并且它的工作精美的制作。

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为好。由于提供的服务不硬codeD,这意味着:

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请求,将它们映射到这些定义,并确保处理以前的请求符合方法签名
  • 一旦反应的处理,创造一个SOAP响应会议WDSL返回结果

在MS文档(和谷歌)使用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;
    }
}

您可以使用此code

you can use this code

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();

不过既然你说

But since you've said

出版在运行中的数据库生成的方法定义一个WSDL

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

您必须创建键入在运行时

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();

现在,您可以使用键入以创建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。 提琴手可以给你关于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天全站免登陆