如何在 Web 服务中传递字典

发布于 2024-11-07 19:10:28 字数 1268 浏览 6 评论 0原文

我创建了一个简单的服务来提供配置选项作为字典。我创建了一个新的 SerializedDictionary 类,它实现了 IXmlSerialized,如本板上所述。当我添加服务时(通过我的消费者项目中的“添加服务引用”),方法签名显示它返回一个数据集。当我在运行时检查数据集时,它是空的。

这是我的服务:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class AppSettingsService : System.Web.Services.WebService
{
        [WebMethod]
        public SerializableDictionary<string, string> ReadAppSettings()
        {
            return AppSettings.Settings; // Settings is a SerializableDictionary<string, string>
        }
}

在客户端,我这样做:

using Clients.AppSettingsServiceReference;

        static AppSettings()
        {
            AppSettingsServiceSoapClient client = new AppSettingsServiceSoapClient();
            var settings = client.ReadAppSettings();
            // ...
        }

我希望 settings 是一个 SerializedDictionary,但它是一个空的数据集。我尝试按照此处和其他地方的建议将其作为 KeyValuePairs 列表发送,但这也返回一个数据集。我正在使用 C#、ASP.Net 3.5、VS2008 Pro 我确信我忽略了一些简单的东西,但我找不到它。有人可以告诉我该怎么做吗?

I have created a simple service to provide configuration options as a Dictionary. I created a new SerializableDictionary class that implements IXmlSerializable as described on this board. When I add the service (via "Add Service Reference" in my consumer project), the method signature shows that it returns a DataSet. When I examine the DataSet at runtime it is empty.

Here is my service:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class AppSettingsService : System.Web.Services.WebService
{
        [WebMethod]
        public SerializableDictionary<string, string> ReadAppSettings()
        {
            return AppSettings.Settings; // Settings is a SerializableDictionary<string, string>
        }
}

On the Client side I do this:

using Clients.AppSettingsServiceReference;

        static AppSettings()
        {
            AppSettingsServiceSoapClient client = new AppSettingsServiceSoapClient();
            var settings = client.ReadAppSettings();
            // ...
        }

I expect settings to be a SerializableDictionary but it's an empty DataSet. I've tried sending it as a List of KeyValuePairs as suggested here and elsewhere but that also returns a DataSet. I'm using C#, ASP.Net 3.5, VS2008 Pro
I'm sure I'm overlooking something simple but I can't find it. Can someone show me how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信仰 2024-11-14 19:10:28

一般来说,任何类型的 Web 服务都不理解特定于您所运行的平台的数据类型。

特别是,没有“字典”的一般概念,因此 Web 服务无法理解它们。

更重要的是,没有办法在 ASMX Web 服务中获取类似于字典的东西。如果您使用 WCF(正如您应该为所有新开发所做的那样),那么您可以在客户端上使用与服务上相同的类型。

当然,如果您的客户端和服务不都运行 .NET,您仍然会遇到同样的问题。

In general, web services of any kind do not understand data types which are specific to the platform you're running on.

In particular, there's no general concept of "dictionary", so web services don't understand them.

More to the point, there's no way to get anything like a dictionary across ASMX web services. If you were using WCF (as you should be doing for all new development), then you could use the same types on the client as on the service.

Of course, you'd still have the same problem if your client and service are not both running .NET.

装纯掩盖桑 2024-11-14 19:10:28

约翰·桑德斯是对的:这实际上是不可能的。完成您想要做的事情的“正确”方法是创建一个类来表示您真正想要返回的键值对,然后返回该类的 IEnumerable 。例如:

public class AppSetting
{
    public string Name {get;set;}
    public string Value {get;set;}
}

    [WebMethod]
    public IEnumerable<AppSetting> ReadAppSettings()
    {
        return AppSettings.Settings.Select(
            kvp => new AppSetting{Name = kvp.Key, Value = kvp.Value});
    }

这直接向消费​​者提供数据:让他们决定是否要将其表示为字典/哈希表、列表、数组、树或他们决定使用的任何其他数据结构。从数据完整性的角度来看,您所关心的只是确保每个键都与正确的值相关联。

John Saunders is right: this isn't really possible. The "right" way to do what you're trying to do is to create a class that represents the key-value pairs you're really trying to return, and then return an IEnumerable of that class. For example:

public class AppSetting
{
    public string Name {get;set;}
    public string Value {get;set;}
}

    [WebMethod]
    public IEnumerable<AppSetting> ReadAppSettings()
    {
        return AppSettings.Settings.Select(
            kvp => new AppSetting{Name = kvp.Key, Value = kvp.Value});
    }

This provides the data straight to the consumer: let them decide whether they want to represent this as a Dictionary/Hashtable, List, Array, Tree, or any other data structure they decide to make of it. All you care about from a data-integrity standpoint is making sure each key is tied to the right value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文