简体   繁体   中英

Call WCF method from client c#

I have created one WCF service Application. There are few methods in Service1.svc .

Here is my IService1.cs

[OperationContract]
GetUserDetailsByEmail_Result GetUserDetailsByEmail(string email);

Here is my 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 . It contain some Scalar Property .

Basically what I am trying to do is, I want to call this method from Client (c#) side. 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 . Here I am simply calling wcf method. It gives me an error

Cannot implicitly convert type 'void' to 'Demo.DemoServiceReference_Client.GetUserDetailsByEmail_Result'

It works when I use svc_GetUserDetailsByEmailCompleted method. But I want the return data directly in dtbUserDetails . How can I achieve this? Is there any changes in my WCF service or in my client side? Or in WCF method declaration?

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.

Bottom line anything not marked is not going over the wire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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