传递集合或数组类型输入参数wcf服务

发布于 2024-11-15 10:30:08 字数 881 浏览 4 评论 0原文

我编写了一个 WCf 服务,它有一个集合类型输入主体参数和另一个参数作为查询字符串,如下所示:

[WebInvoke(Method = "PUT", UriTemplate = "users/role/{userID}",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool AssignUserRole(int userID,Collection<int> roleIDs)
{
    //do something
    return restult;
}

现在,当我尝试传递此参数时,我收到反序列化错误。我尝试过以下格式:

<AssignUserRole xmlns="http://tempuri.org/">
 <roleIDs>
  <roleID>7</roleID>
 </roleIDs>
</AssignUserRole>

<AssignUserRole xmlns="http://tempuri.org/">
 <ArrayOfroleID>
  <roleID>7</roleID>
 </ArrayOfroleID>
</AssignUserRole>

<AssignUserRole xmlns="http://tempuri.org/">
 <ArrayOfint> 
  <int>7</int>
 </ArrayOfint>
</AssignUserRole>

有人可以帮我如何传递这个数组(集合类型主体参数)吗?

谢谢。

I have written a WCf Service which has a Collection type input body parameter and another parameter as query string as following:

[WebInvoke(Method = "PUT", UriTemplate = "users/role/{userID}",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
[OperationContract]
public bool AssignUserRole(int userID,Collection<int> roleIDs)
{
    //do something
    return restult;
}

Now when I am trying to pass this parameter I am getting de serializing error. I have tried following format:

<AssignUserRole xmlns="http://tempuri.org/">
 <roleIDs>
  <roleID>7</roleID>
 </roleIDs>
</AssignUserRole>

<AssignUserRole xmlns="http://tempuri.org/">
 <ArrayOfroleID>
  <roleID>7</roleID>
 </ArrayOfroleID>
</AssignUserRole>

<AssignUserRole xmlns="http://tempuri.org/">
 <ArrayOfint> 
  <int>7</int>
 </ArrayOfint>
</AssignUserRole>

Can some one help me how can I pass this Array(Collection type Body parameter)?

Thanks.

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

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

发布评论

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

评论(1

〃温暖了心ぐ 2024-11-22 10:30:08

正确的格式是这样的:

<AssignUserRole xmlns="http://tempuri.org/">
  <roleIDs xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">     
    <a:int>7</a:int>
    <a:int>8</a:int>
  </roleIDs>
</AssignUserRole>

找出某个操作的预期格式的一种简单方法是使用具有相同合约的 WCF 客户端,用它发送消息并使用 Fiddler 查看操作。下面的程序就是这样做的。

public class StackOverflow_6339286
{
    [ServiceContract]
    public interface ITest
    {
        [WebInvoke(Method = "PUT", UriTemplate = "users/role/{userID}", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        [OperationContract]
        bool AssignUserRole(string userID, Collection<int> roleIDs);
    }
    public class Service : ITest
    {
        public bool AssignUserRole(string userID, Collection<int> roleIDs)
        {
            return true;
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
        ITest proxy = factory.CreateChannel();

        proxy.AssignUserRole("1234", new Collection<int> { 1, 2, 3, 4 });

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

另请注意,您的 UriTemplate 中存在问题:路径变量 {userId} 不能为 int 类型(它必须是字符串)。这已在上面的示例代码中修复。

另一件事:如果您不想使用集合/数组的默认命名空间,您可以使用 [CollectionDataContract] 类来更改它。如果您使用下面的类而不是使用 Collection,那么您尝试的第一个主体应该可以工作:

[CollectionDataContract(Namespace = "http://tempuri.org/", ItemName = "roleID")]
public class MyCollection : Collection<int> { }

The correct format would be this:

<AssignUserRole xmlns="http://tempuri.org/">
  <roleIDs xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">     
    <a:int>7</a:int>
    <a:int>8</a:int>
  </roleIDs>
</AssignUserRole>

One easy way to find out what the expected format is for a certain operation is to use a WCF client with the same contract, send a message with it and look at the operation using Fiddler. The program below does that.

public class StackOverflow_6339286
{
    [ServiceContract]
    public interface ITest
    {
        [WebInvoke(Method = "PUT", UriTemplate = "users/role/{userID}", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        [OperationContract]
        bool AssignUserRole(string userID, Collection<int> roleIDs);
    }
    public class Service : ITest
    {
        public bool AssignUserRole(string userID, Collection<int> roleIDs)
        {
            return true;
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Host opened");

        WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(new Uri(baseAddress));
        ITest proxy = factory.CreateChannel();

        proxy.AssignUserRole("1234", new Collection<int> { 1, 2, 3, 4 });

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

Also notice that there's a problem in your UriTemplate: the path variable {userId} cannot be of type int (it must be a string). This is fixed in the sample code above.

One more thing: if you don't want to use the default namespace for collections / arrays, you can use a [CollectionDataContract] class to change it. If instead of using Collection you used the class below, then the first body you tried should work:

[CollectionDataContract(Namespace = "http://tempuri.org/", ItemName = "roleID")]
public class MyCollection : Collection<int> { }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文