简体   繁体   English

WCF服务创建单独的代理类

[英]WCF service creating separate proxy class

I have a WCF service method that sends back a MembershipCreateStatus ( System.Web.Security ) to the calling method. 我有一个WCF服务方法,该方法将MembershipCreateStatusSystem.Web.Security )发送回调用方法。 When I look at the service definition it has recreated the enum as a type of MyProject.MyWebService.MembershipCreateStatus so it is essentially a completely different object. 当我查看服务定义时,它已将枚举重新创建为MyProject.MyWebService.MembershipCreateStatus类型,因此它实际上是一个完全不同的对象。

Is there a way in which I can tell the service definition to use the System.Web.Security class instead, even though it is this within the WCF service? 有没有一种方法可以告诉服务定义使用System.Web.Security类,即使它在WCF服务中也是如此?

You you can. 你可以的 You need to use the KnownTypeAttribute class to decorate the DataContract in your WCF Service to specify that the enum is of type System.Web.Security.MembershipCreateStatus I'm not aware of the details of your environment but in general unless you control both the WCF service and the consuming clients, I would carefully research the support requirements and the possibility of a future change to the enum causing backwards compatibility issues with clients that are consuming this enum. 您需要使用KnownTypeAttribute类来装饰WCF服务中的DataContract,以指定枚举的类型为System.Web.Security.MembershipCreateStatus我不了解您的环境的详细信息,但是通常除非您同时控制两个WCF服务和使用该客户端的客户,我会仔细研究支持要求以及对该枚举的将来更改的可能性,从而导致与使用该枚举的客户端的向后兼容性问题。 Also to consider is a scenario where a non .NET client could consume your WCF service. 还需要考虑一个非.NET客户端可以使用您的WCF服务的方案。 In that case you need to consider if using the System.Web.Security.MembershipCreateStatus enum is a good idea versus implementing your own statuses for Member creation. 在那种情况下,您需要考虑使用System.Web.Security.MembershipCreateStatus枚举是否是一个好主意,而不是为成员创建实现自己的状态。 Here is another question on StackOverflow with a good discussion on this topic. 是有关StackOverflow的另一个问题,并对这个主题进行了很好的讨论。

For example see the following code below 例如,请参见下面的以下代码

[ServiceContract]
public interface IMembershipService
{
    [OperationContract]
    CreateMemberResponse CreateMember(ApplicationUser userToCreate);
}

[DataContract]
[KnownType(typeof(System.Web.Security.MembershipCreateStatus))]
public class CreateMemberResponse
{
    [DataMember]
    public MembershipCreateStatus Status { get; set; }
}

[DataContract]
public class ApplicationUser
{
    public bool ReturnSuccess { get; set; }

    public ApplicationUser()
    {
        ReturnSuccess = true;
    }
}

You can write a test against this service as follows and this test will succeed. 您可以按如下所示对此服务编写测试,该测试将成功。

[TestClass]
public class MembershipStatusInvocationTests
{
    [TestMethod]
    public void CreateMemberShouldReturnMembershipCreateStatusEnum()
    {
        var client = new MembershipServiceClient();
        var response = client.CreateMember(new ApplicationUser {ReturnSuccess = true});

       Assert.IsInstanceOfType(response.Status, typeof(System.Web.Security.MembershipCreateStatus));
    }
}

For more information on the KnownTypeAttribute class see here 有关KnownTypeAttribute类的更多信息,请参见此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM