可以将Web服务中定义的CONSTANTS暴露给使用客户端(.Net双方)

时间:2022-02-19 11:11:28

I have several constant defined for a webservice, and I would like to make these available to consuming client. Both the server and client are .Net.

我为web服务定义了几个常量,我想让这些客户端可以使用它们。服务器和客户端都是.Net。

I think it is possible to achieve this by using enum's, but many of my constants are text string with spaces, so I would have to write an extra function to convert the enum into the equivalent text string.

我认为可以通过使用枚举来实现这一点,但我的许多常量都是带空格的文本字符串,所以我必须编写一个额外的函数来将枚举转换为等效的文本字符串。

So, is there any way of defining the constants in the web service so they would then be available to the client??

那么,有没有什么方法可以定义Web服务中的常量,以便它们随后可供客户端使用?

Update (Kev): I want to expose them to the client via the web service itself, not via a separate assembly. Update #2 (Paige): So if I understand you, I will then have a new List object containing the constants, but how does the client use that? Wouldn't it look like (roughly): dim constants as List = mywebservice.GetConstants() dim someresult as Integer = mywebservice.somefunction(constants(3)) Unless I misunderstand you, that totally defeats the point of defining constants.

更新(Kev):我想通过Web服务本身将它们暴露给客户端,而不是通过单独的程序集。更新#2(Paige):所以,如果我理解你,那么我将有一个包含常量的新List对象,但是客户端如何使用它?看起来不像(粗略地):暗淡的常量如List = mywebservice.GetConstants()dim someresult as Integer = mywebservice.somefunction(constants(3))除非我误解你,否则完全违背了定义常量的观点。

5 个解决方案

#1


The answer seems be.....cannot be done.

答案似乎是.....无法完成。

#2


You could implement these constants in a standalone assembly and reference the assembly from both the web service and the client.

您可以在独立程序集中实现这些常量,并从Web服务和客户端引用程序集。

#3


I don't know that there's any way to expose constants per se.

我不知道有什么方法可以暴露常数本身。

But perhaps you could just implement some functions which always return the same value and just give them a special naming convention? For example:

但也许你可以只实现一些总是返回相同值的函数,并给它们一个特殊的命名约定?例如:

public int FLAG_READONLY()
{
   return 3;
}

I may be misunderstanding your need.

我可能误解了你的需要。

#4


You could write the "constants" into a dictionary and then have a web method that returns the keys.

您可以将“常量”写入字典,然后使用返回键的Web方法。

Or, using Kev's answer above:

或者,使用Kev的答案:

Class:

public class Win32Message   {
    public const int WM_ACTIVATE =0x0006;
    public const int WM_ACTIVATEAPP = 0x001C;
    public const int WM_AFXFIRST = 0x0360;
    public const int WM_AFXLAST = 0x037F;
    public const int WM_APP = 0x8000;
    public const int WM_ASKCBFORMATNAME        = 0x030C;
}

And then in the web service use a web method like:

然后在Web服务中使用Web方法,例如:

[WebMethod]
public System.Collections.Generic.List<string> GetConstants(System.Type type)
{
    System.Collections.Generic.List<string> constants = new 
        System.Collections.Generic.List<string>();

    System.Reflection.FieldInfo[] fieldInfos = type.GetFields(
        System.Reflection.BindingFlags.Public | BindingFlags.Static |
        System.Reflection.BindingFlags.FlattenHierarchy);

    // Go through the list and only pick out the constants
    foreach (System.Reflection.FieldInfo fi in fieldInfos)
    if (fi.IsLiteral && !fi.IsInitOnly)
        constants.Add(fi.Name);

    return constants;
}

I found this on Wes' Puzzling Blog

我在Wes的Puzzling Blog上找到了这个

Of course, you can return it as an array, arraylist or however you'd like.

当然,您可以将其作为数组,arraylist或者您喜欢的方式返回。

#5


As i see it, the real point of webservices is being able to retrieve data or execute processes on a server in a LANGUAGE and MACHINE-independent way, using xml or json or whatever as the language for data representation. When you call a service to list products, you just want to get the products, you don't want (or need) to know if you're calling a stored SQL procedure or a C# business function in the server. So i don't think it makes sense to share 'constants' from the client to the server unless you have a web method that lists those constants to you (again, in a text-only way). I think the whole Microsoft web service .asmx stack just sucks because it relies on the 'remote procedure call' metaphor instead of the 'service' part of the web service concept.

正如我所看到的,webservices的真正意义在于能够以与LANGUAGE和MACHINE无关的方式在服务器上检索数据或执行进程,使用xml或json或任何作为数据表示的语言。当您调用服务列出产品时,您只想获取产品,您不希望(或需要)知道您是在服务器中调用存储的SQL过程还是C#业务功能。所以我认为将'常量'从客户端共享到服务器是没有意义的,除非你有一个web方法向你列出这些常量(再次,以纯文本方式)。我认为整个Microsoft Web服务.asmx堆栈很糟糕,因为它依赖于“远程过程调用”隐喻而不是Web服务概念的“服务”部分。

I agree this is subjective :P

我同意这是主观的:P

#1


The answer seems be.....cannot be done.

答案似乎是.....无法完成。

#2


You could implement these constants in a standalone assembly and reference the assembly from both the web service and the client.

您可以在独立程序集中实现这些常量,并从Web服务和客户端引用程序集。

#3


I don't know that there's any way to expose constants per se.

我不知道有什么方法可以暴露常数本身。

But perhaps you could just implement some functions which always return the same value and just give them a special naming convention? For example:

但也许你可以只实现一些总是返回相同值的函数,并给它们一个特殊的命名约定?例如:

public int FLAG_READONLY()
{
   return 3;
}

I may be misunderstanding your need.

我可能误解了你的需要。

#4


You could write the "constants" into a dictionary and then have a web method that returns the keys.

您可以将“常量”写入字典,然后使用返回键的Web方法。

Or, using Kev's answer above:

或者,使用Kev的答案:

Class:

public class Win32Message   {
    public const int WM_ACTIVATE =0x0006;
    public const int WM_ACTIVATEAPP = 0x001C;
    public const int WM_AFXFIRST = 0x0360;
    public const int WM_AFXLAST = 0x037F;
    public const int WM_APP = 0x8000;
    public const int WM_ASKCBFORMATNAME        = 0x030C;
}

And then in the web service use a web method like:

然后在Web服务中使用Web方法,例如:

[WebMethod]
public System.Collections.Generic.List<string> GetConstants(System.Type type)
{
    System.Collections.Generic.List<string> constants = new 
        System.Collections.Generic.List<string>();

    System.Reflection.FieldInfo[] fieldInfos = type.GetFields(
        System.Reflection.BindingFlags.Public | BindingFlags.Static |
        System.Reflection.BindingFlags.FlattenHierarchy);

    // Go through the list and only pick out the constants
    foreach (System.Reflection.FieldInfo fi in fieldInfos)
    if (fi.IsLiteral && !fi.IsInitOnly)
        constants.Add(fi.Name);

    return constants;
}

I found this on Wes' Puzzling Blog

我在Wes的Puzzling Blog上找到了这个

Of course, you can return it as an array, arraylist or however you'd like.

当然,您可以将其作为数组,arraylist或者您喜欢的方式返回。

#5


As i see it, the real point of webservices is being able to retrieve data or execute processes on a server in a LANGUAGE and MACHINE-independent way, using xml or json or whatever as the language for data representation. When you call a service to list products, you just want to get the products, you don't want (or need) to know if you're calling a stored SQL procedure or a C# business function in the server. So i don't think it makes sense to share 'constants' from the client to the server unless you have a web method that lists those constants to you (again, in a text-only way). I think the whole Microsoft web service .asmx stack just sucks because it relies on the 'remote procedure call' metaphor instead of the 'service' part of the web service concept.

正如我所看到的,webservices的真正意义在于能够以与LANGUAGE和MACHINE无关的方式在服务器上检索数据或执行进程,使用xml或json或任何作为数据表示的语言。当您调用服务列出产品时,您只想获取产品,您不希望(或需要)知道您是在服务器中调用存储的SQL过程还是C#业务功能。所以我认为将'常量'从客户端共享到服务器是没有意义的,除非你有一个web方法向你列出这些常量(再次,以纯文本方式)。我认为整个Microsoft Web服务.asmx堆栈很糟糕,因为它依赖于“远程过程调用”隐喻而不是Web服务概念的“服务”部分。

I agree this is subjective :P

我同意这是主观的:P