简体   繁体   English

从客户端C#调用WCF方法

[英]Call WCF method from client c#

I have created one WCF service Application. 我创建了一个WCF服务应用程序。 There are few methods in Service1.svc . Service1.svc方法很少。

Here is my IService1.cs 这是我的IService1.cs

[OperationContract]
GetUserDetailsByEmail_Result GetUserDetailsByEmail(string email);

Here is my Service.svc.cs 这是我的Service.svc.cs

public class Service1 : IService1
{
    #region GetUserDetails
    public GetUserDetailsByEmail_Result GetUserDetailsByEmail(string email)
    {
        return (new UserManager()).GetUserDetailsByEmail(email);
    }
    #endregion    
}

Here GetUserDetailsByEmail_Result is Complex type created in DemoModel.edmx . 在这里, GetUserDetailsByEmail_Result是在DemoModel.edmx中创建的Complex type It contain some Scalar Property . 它包含一些标量属性

Basically what I am trying to do is, I want to call this method from Client (c#) side. 基本上我想做的是,我想从客户端 (c#)端调用此方法。 Here is my Client Side code 这是我的客户端代码

//svc.GetUserDetailsByEmailCompleted += new EventHandler<GetUserDetailsByEmailCompletedEventArgs>(svc_GetUserDetailsByEmailCompleted);
GetUserDetailsByEmail_Result dtbUserDetails = svc.GetUserDetailsByEmailAsync(loginName);

Here svc is the object of Service1Client . svcService1Client的对象。 Here I am simply calling wcf method. 在这里,我只是在调用wcf方法。 It gives me an error 它给我一个错误

Cannot implicitly convert type 'void' to 'Demo.DemoServiceReference_Client.GetUserDetailsByEmail_Result' 无法将类型'void'隐式转换为'Demo.DemoServiceReference_Client.GetUserDetailsByEmail_Result'

It works when I use svc_GetUserDetailsByEmailCompleted method. 当我使用svc_GetUserDetailsByEmailCompleted方法时,它可以工作。 But I want the return data directly in dtbUserDetails . 但是我想直接在dtbUserDetails返回数据。 How can I achieve this? 我该如何实现? Is there any changes in my WCF service or in my client side? WCF服务或客户端是否有任何更改? Or in WCF method declaration? 还是在WCF方法中声明?

You either need to create an object and bind the data to it like some of the people in the comments suggested then mark each property like so: 您要么需要创建一个对象并将数据绑定到它,就像建议的注释中的某些人一样,然后像这样标记每个属性:

[DataContract(Namespace = "MyServiceContract.Service1.ComplexObject")]
public class ComplexObject
{
       [DataMember(Order = 1, IsRequired = true)]
            public String DbItem1{ get; private set; }
       [DataMember(Order = 2, IsRequired = false)]
            public ComplexBlobData DbItem2{ get; set; }
}

Or if you can open up the DemoModel.edmx(Code Behind) and mark it all with data contract the same way you would mark your own object. 或者,如果您可以打开DemoModel.edmx(Code Behind)并用数据契约标记所有标记,就像标记自己的对象一样。

Bottom line anything not marked is not going over the wire. 底线未标记的任何内容都不会越过导线。

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

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