Value passing to WCF Service using the default generated WCF code

When you create a WCF Service, There are 2 code files created IService.cs and Service.cs.

IService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the “Rename” command on the “Refactor” menu to change the interface name “IService” in both code and config file together.
[ServiceContract]
public interface IService
{
         [OperationContract]
         string GetData(int value);
 
         [OperationContract]
         CompositeType GetDataUsingDataContract(CompositeType composite);
         // TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
         bool boolValue = true;
         string stringValue = “Hello “;

         [DataMember]
         public bool BoolValue
         {
            get { return boolValue; }
            set { boolValue = value; }
         }
         [DataMember]
         public string StringValue
         {
            get { return stringValue; }
            set { stringValue = value; }
         }
}

Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// NOTE: You can use the “Rename” command on the “Refactor” menu to change the class name “Service” in code, svc and config file together.

public class Service : IService
{
     public string GetData(int value)
     {
             return string.Format(“You entered: {0}”, value);
     }
     public CompositeType GetDataUsingDataContract(CompositeType composite)
     {
             if (composite == null)
             {
                      throw new ArgumentNullException(“composite”);
             }
             if (composite.BoolValue)
             {
                     composite.StringValue += “Suffix”;
             }
             return composite;
     }
}

Open another instance of Visual Studio 2010. Create an Empty Web Project, set Service Reference to the above WCF Service.

Add a WebForm. Drag a Button from Toolbox to WebForm.
<body>
  <form id=”form1″ runat=”server”>
   <div>
    <asp:Button ID=”Button1″ runat=”server” Text=”Button” OnClick=”Button1_Click” />
   </div>
  </form>
</body>

Add the code to Button Click event as below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebAppWCFClient
{
  public partial class WebForm1 : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      ServiceReference1.ServiceClient ws = new ServiceReference1.ServiceClient();
      ServiceReference1.CompositeType wsd = new ServiceReference1.CompositeType();
      wsd.StringValue = “HRIS”;
     //wsd = ws.GetDataUsingDataContract(wsd);
      Response.Write(wsd.StringValue);
   }
}
}

Download the complete code from http://skydrive.live.com. The sample file name is SelfHostWCF.rar. My MSN ID is chanmmn@hotmail.com.
Advertisements

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress (http://www.pcworld.com/article/206455/Microsoft_Live_Spaces_Moves_to_WordPress_An_FAQ.html) till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net. Bookmark the permalink.

5 Responses to Value passing to WCF Service using the default generated WCF code

  1. javier says:

    how to consume the GetDataUsingDataContract in vb6. I’m using soap 3. I’m able to consume the getdata. but I’m trying to create the class as follow
    **********************
    Private BoolValueField As Boolean
    Private StringValueField As String
    Public Property Let StringValue(ByVal vData As String)
    StringValueField = vData
    End Property
    Public Property Get StringValue() As String
    StringValue = StringValueField
    End Property
    Public Property Let BoolValue(ByVal vData As Boolean)
    BoolValueField = vData
    End Property
    Public Property Get BoolValue() As Boolean
    BoolValue = BoolValueField
    End Property
    ****************************
    this is my vb code
    ******************
    Option Explicit
    Private test As New CompositeType
    Private test2 As New CompositeType
    Private Sub Command1_Click()
    Dim oSOAP As New MSSOAPLib30.SoapClient30
    oSOAP.ClientProperty(“ServerHTTPRequest”) = True
    Call oSOAP.MSSoapInit(“http://localhost/WCFService1/service.svc?wsdl”)
    MsgBox (oSOAP.GetData(5)) ‘ works fine

    test.BoolValue = True
    test.StringValue = “hola”
    test2 = oSOAP.GetDataUsingDataContract(test) ‘ shows the error
    End Sub
    ***************
    I think this have to do with Private extensionDataField As System.Runtime.Serialization.ExtensionDataObject
    but I dont know how to do this in vb6.

    btw I have a console aplication in vb.net and works fine. but I want to use it in vb6. is this posible for composite ?

  2. chanmingman says:

    I have put my VB6 behind since year 2002.

    Regards

  3. any other sample programe send for wcf service in c sharp

  4. Hi
    how to implement the interface in asp.net c sharp and easy to learn sample programe

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s