WCF

Windows Communication Foundation (WCF) is a tool used to implement and deploy a service-oriented architecture (SOA) to support distributed computing where services have remote consumers. It features a runtime and a set of APIs in the .NET Framework for building connected, service-oriented applications. Services typically have a WSDL interface (Web Services Description Language) that any WCF client can use to consume the service, regardless of which platform the service is hosted on. WCF implements many advanced Web services (WS) standards such as WS-Addressing, WS-ReliableMessaging, WS-Security, RSS Syndication Services, WS-Discovery, routing and better support for REST services.

Share tech news, updates, or what's on your mind.

Sign up to Post

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something gets changed at service? Do we need to regenerate the proxy files? Yes of course, we have to. How to over come such limitation? Here comes WCF handy in creating proxies at runtime. So we need not to create proxy using Add Service Reference option.
 
Actually we are not going to create proxy at runtime, we are just invoking calls to WCF service using ChannelFactory class which is available under System.ServiceModel namespace. But to achieve this we need to expose the Interface (which defines the service and operation) to the client. This option is more suitable if both the service and client is in your control.

Ok lets start implementing the same. I'll create a small calculator service.

Define an Service Contract and Operation Contract. Create an separate class library so that it can be exposed to client.
namespace CalculatorServiceContract
{
  [ServiceContract]
  public interface ICalculator 
  {
    [OperationContract]
    int Add (int a, int b);
  }
}

Open in new window


Next step is to implement the Interface in a class. Add reference to the CalculatorServiceContract dll in the Service class project. And implement as follows

namespace CalculatorService
{

    // Implementation of CalculatorServiceContract 

    public class CalculatorService : ICalculator
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
   }
}

Open in new window


Next step is to configure web.config at service.

Open in new window

1
Free Tool: ZipGrep
LVL 10
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

While working on Silverlight and WCF application, I faced one issue where fault exception occurred at WCF operation contract is not getting propagated to Silverlight client. So after searching net I came to know that it was behavior by default for silverlight client. And these issues can be addressed by little tweak. So, here I am writing on how to propagate fault exception from service to silverlight client.

Reason for this kind of behavior is if any fault exception is raised at operation contract, then the exception is converted to SOAP faults and returned to the client. Since the networking in Silverlight is performed based on browser's HTTP stack. But this limitation is applicable to Silverlight 2. In Silverlight 3, the networking is performed using browser HTTP stack by default. But the new client HTTP stack allows you to bypass the browser and perform networking operation through operating system.

In Silverlight 2, SOAP faults cannot be propagated to client side because due to limitations in silverlight, it cannot access some of the messages in the browser HTTP stack bodies. So HTTP error code 500 which holds the SOAP faults is converted to 404(Page not found), and same is returned to client. So obviously client will be receiving "Remote Server not found"  exception.

In Silverlight 3, this limitation is solved. Since Silverlight 3 uses client HTTP stack, it is simple that networking is performed by API's exposed by operating system itself. So you can tell …
0
In previous Articles, we have discussed how we can upload a file using .asmx web service and isolated storage space. Here, in continuation to the topic, I am going to discuss how we can use WCF for the same purpose.

Steps:
1.Create the silverlight project with FileUploadWCF as the name.
2.Make these important changes in the properties of the FileUploadWCF.Web project:
   a.Right-click on FileUploadWCF.Web.
   b.Select Properties.
   c.Under the Web tab, Select Specific port and give the port number (e.g., I am using port number 1366).

Note: we are using specific port so that every time we need not to run the
web service its address will be unchanged. Either we need to host service on IIS if you don t want to use specific port or update the service reference each time before running the application.


    d.Save The Properties Page and close it.
3.Now Add a silverlight compatible WCF to the Project and name it as uploadService.
4.Include the name space System.IO :
using System.IO;

Open in new window


5.Add an operation Contract as :
[OperationContract]
public List<string> UploadFiles(string strFileName,byte[] byFile, string strFileNo)
{
    List<string> result = new List<string>();
    try
   {
        //if file length is greater than 0    
        if (byFile.Length > 0)
       {
            //set the location in a string variable where to save the file
           string 

Open in new window

2
 
LVL 60

Expert Comment

by:Kevin Cross
Comment Utility
Nice work, Meetu.
Voted yes above!
0
This tutorial will show you how to add an attribute to an XML stream returned from a Windows Communication Foundation (WCF) Web Service.  Some knowledge of WCF and XML is required; the code is in C#.  Below is an XML sample of an attributed XML stream comprised of an array of dynamically placed arguments.
<info xmlns="http://myns.com">
  <args>
    <arg type="firstname">
      <value>William</value>
    </arg>
    <arg type="lastname">
      <value>Campbell</value>
    </arg>
</info>

Open in new window


Unfortunately, there is *no way* to get this exact piece of XML is in the current version of WCF as attributes in XML streams are not supported using DataContract!

The solution to follow uses the 'KnownType' WCF attribute to piggyback the Microsoft 'i:type' attribute.  Using the method described in this article, we can get close to the above (desired) XML as the output we get looks like this:
<info xmlns="http://myns.com" 
  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <args>
    <arg i:type="firstname"> 
      <value>William</value>
    </arg>
    <arg i:type="lastname">
      <value>Campbell</value>
    </arg>
</info>

Open in new window


Notice the difference is the addition of namespace 'i' that changes 'type' to 'i:type'.  If you can live with the 'i:' prefix, this solution is for you.

With that said, let's get under way with a sample definition of a WCF service that will use my technique.
using System;
using System.ServiceModel;
using System.ServiceModel.Web;

[ServiceContract]
[DataContractFormat]
public interface IRPCService
{
    [OperationContract]
    [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "/getuserxml?id={userID}")]
    info GetUserXML(String userID);
}

Open in new window


GetUserXML is a function declaration in a .svc file.  It is a RESTful (REpresentational State Transfer) Web Service call which would be invoked like this.
http://www.experts-exchange.com/RPCService.svc/getuserxml?id=williamcampbell

Open in new window


The function returns an 'info' class as 'plain' serialized XML to the caller.  Within the plain serialized XML will be the embedded attributes.  Each attribute that will appear in the serialized XML must be declared as a KnownType and have its own class implementation.  The code below shows a base class that defines two KnownTypes.

Open in new window

1

WCF

Windows Communication Foundation (WCF) is a tool used to implement and deploy a service-oriented architecture (SOA) to support distributed computing where services have remote consumers. It features a runtime and a set of APIs in the .NET Framework for building connected, service-oriented applications. Services typically have a WSDL interface (Web Services Description Language) that any WCF client can use to consume the service, regardless of which platform the service is hosted on. WCF implements many advanced Web services (WS) standards such as WS-Addressing, WS-ReliableMessaging, WS-Security, RSS Syndication Services, WS-Discovery, routing and better support for REST services.

Top Experts In
WCF
<
Monthly
>