简体   繁体   English

C#强制转换子类SOAP

[英]c# casting child class SOAP

I have a soap Interface with a Method "GetCustomer", which returns a customerDto. 我有一个带有方法“ GetCustomer”的肥皂接口,该方法返回一个customerDto。 I wanted to create a new Interface method for another Client with more infos so I created a second method"GetCustomerExtended" which returns a customerExtendedDto with the customer object as a parent. 我想为具有更多信息的另一个客户端创建一个新的接口方法,因此我创建了第二个方法“ GetCustomerExtended”,该方法返回一个以客户对象为父对象的customerExtendedDto。 To reuse my code I cast the child to the parent, but it seems not working. 为了重用我的代码,我将孩子强制转换为父母,但似乎不起作用。 The soap doesn't return any infos. 肥皂不返回任何信息。 Here is my code: 这是我的代码:

soap class: 肥皂类:

public class ShopService : IShopService
{
    public CustomerDto GetCustomer(int id)
    {
        return GetCustomerExtended(id);
    }

    public CustomerExtendedDto GetCustomerExtended(int id)
    {
        // Fill result
        CustomerExtendedDto result = new CustomerExtendedDto();
        result.Id = 1;
        result.Name = "foo";
        result.Surname = "bar";

        return result;
    }
}

[ServiceContract]
public interface IShopService
{
        [OperationContract]
        [FaultContract(typeof(InvalidParameterFaultMessage))]
        [FaultContract(typeof(DataNotFoundFaultMessage))]
        [FaultContract(typeof(InvalidOperationFaultMessage))]
        [FaultContract(typeof(ApplicationLogicFaultMessage))]
        [FaultContract(typeof(NotAvailableFaultMessage))]
        [FaultContract(typeof(TimeoutFaultMessage))]
        CustomerDto GetCustomer(int id);

        [OperationContract]
        [FaultContract(typeof(InvalidParameterFaultMessage))]
        [FaultContract(typeof(DataNotFoundFaultMessage))]
        [FaultContract(typeof(InvalidOperationFaultMessage))]
        [FaultContract(typeof(ApplicationLogicFaultMessage))]
        [FaultContract(typeof(NotAvailableFaultMessage))]
        [FaultContract(typeof(TimeoutFaultMessage))]
        CustomerExtendedDto GetCustomerExtended(int id);
}

Dto: Dto:

[DataContract]
public class CustomerDto
{
    [DataMember]
    public int Id { get; set; }

    [DataMember]
    public string Name { get; set; }
}

[DataContract]
public class CustomerExtendedDto : CustomerDto
{
    [DataMember]
    public strint Surname{ get; set; }
}

The method seems working and it doesn't throw any expensions, but the answer is always empty. 该方法似乎可行,并且不会花费任何费用,但答案始终是空的。 GetCustomerExtended works perfectly fine, GetCustomer is empty. GetCustomerExtended可以正常工作,GetCustomer为空。 SOAP UI doesn't seems to get any infos. SOAP UI似乎没有任何信息。 If I fill the customer info in "GetCustomer" like this, it works: 如果我像这样在“ GetCustomer”中填写客户信息,则它可以正常工作:

public CustomerDto GetCustomer(int id)
{
    // Fill result
    CustomerDto result = new CustomerDto();
    result.Id = 1;
    result.Name = "foo";

    return result;
}

What's the problem here? 这是什么问题 How can I cast here properly? 我如何才能正确地投射到这里?

the SoapUI log says: Thu Oct 11 11:29:42 CEST 2018:ERROR:Exception in request: java.net.SocketException: Connection reset Thu Oct 11 11:29:42 CEST 2018:ERROR:An error occurred [Connection reset], see error log for details Thu Oct 11 11:29:42 CEST 2018:INFO:Error getting response for [BasicHttpBinding_IShopService.GetCustomer:Request 1]; SoapUI日志说:Thu Oct 11 11:29:42 CEST 2018:ERROR:请求中的异常:java.net.SocketException:连接重置Thu Oct 11 11:29:42 CEST 2018:ERROR:发生错误[连接重置] ,请参阅错误日志以获取详细信息。Thu Oct 11 11:29:42 CEST 2018:INFO:获取[BasicHttpBinding_IShopService.GetCustomer:Request 1]响应时出错; java.net.SocketException: Connection reset java.net.SocketException:连接重置

I found a solution, which isn't really nice, but works: 我找到了一个解决方案,虽然不是很好,但可以:

    public CustomerDto GetCustomer(int id)
    {
        CustomerDto customer = Mapper.Map<CustomerExtendedDto, CustomerDto>(GetCustomerExtended(id));

        return customer;
    }

I think the problem is, that the object is still a "CustomerExtended" object. 我认为问题在于,该对象仍然是“ CustomerExtended”对象。 It's call by reference and I give the customerextended xml back to soap UI(or maybe a mix). 这是通过引用调用的,我将customerextended xml返回到soap UI(或者可能是混合的)。 With automapper I create a CustomerDto and it works fine. 使用自动映射器,我创建了一个CustomerDto,并且运行良好。

But I'm still happy for a better solution for this problem. 但是我仍然很高兴为这个问题提供更好的解决方案。

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

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