Standard Implementation Model

1.) Create a WCF Service Client or use an existing client.

You probably have an existing WCF client laying around that you need to get data from; common clients are generated from WSDL's in VS.NET.

  [ServiceContract]
    public interface IMyClient
    {
        [OperationContract]
        string GetMyResult(string param);
    }

    public class MyClient : ClientBase<MyClient>
    {
        public string GetMyResult(string param)
        {
            return "Success";
        }
    }

2.) Create your objects for the client request and response:

A.) Request

When creating the request to a WCF Client, you will need to construct two object types that will be mapped to the client request.
  • Authentication Class - defines the parameters to authenticate against the WCF client. (Optional)
  • Request Context Class - defines the parameters for the request being performed.

Authentication

Create a class that defines the authentication parameters against the WCF client. This is optional if the client does not require authentication.

    public class MyToken
    {
        public string Value { get; set; }
    }

Request Context

Create a class that derives from the Provider Context<T>. Generic type T is the authentication type you just created. If no message is provided by the client, a string can be used in it's place.

    public class MyContext : ProviderContext<MyToken>
    {
        public MyContext(MyToken token) : base(token) { }
        public string Param { get; set; }
    }

B.) Response

When a response occurs from the WCFClient, in most situations the response package includes a result or possible exception or a result with a message. To capture the response being pushed from the client you will need to create three object types to map to the client response.
  • Message class - defines the message from the client.
  • Result Class- defines the result set from the response. Generally contains the client.
  • Exception Class - defines the exception thrown by the WCFClient when errors occur.

Message

Defines a message that will be pushed from the client, generally occurs if a response had invalid data.

     public class MyMessage
     {
         public string Message { get; set; }
     }

Response Result

The custom result type from the response. Derived from ProviderResult<T>. Generic type T is the message type from the client, generally found on the result. If no message type is provided by the client, a string can be used in it's place.

     public class MyResult : ProviderResult< MyMessage > 
     { 
          public string Value { get; set; } 
     }

Exception

Create a custom type derived from ProviderException. Thrown when an unexpected operation happens. An example is 404 error - not found.

       public class MyException : ProviderException { }

3.) Implement the Controller, invoke a client and make the request.

This is the heart of the implementation - the RxController, the steps performed in parts one and two are all used in part three.
  • Create a client controller that will pull responses from the WCF Client by invoking the RxController using your custom types.

    public class RxImplementationController
    {
        public MyResult GetValues()
        {
            MyResult result = null;

            //Create a new instance of the RxController, assign the event handlers
            var controller = new RxController();
            controller.OnProviderRequest += new Action<IProviderTransaction>(controller_OnProviderRequest);
            controller.OnProviderResponse += new Action<IProviderTransaction>(controller_OnProviderResponse);
            
            var client = new MyClient();

            //Execute the request - pull the results!
            controller.ExecuteRequestAsync<MyContext, MyToken, MyMessage, MyException, MyClient>(
                new MyContext(new MyToken { Value = "MyToken" }), //<-- Request context
                (MyContext c) =>  //<-- Request validation
                {
                    if (c.Token.Value != "myToken")
                        throw new Exception("Token is incorrect");
                },
                (MyContext c) => //<-- Make client call, map value and return result
                {
                    //Make client call here, pass the token
                    return new MyResult { Value = client.GetMyResult(c.Param) };
                },
                (IProviderResult<MyMessage> r) => //Async result
                {
                    result = (MyResult)r;
                },
                client);

            return result;
        }

        void controller_OnProviderResponse(IProviderTransaction obj)
        {
            throw new NotImplementedException();
        }

        void controller_OnProviderRequest(IProviderTransaction obj)
        {
            throw new NotImplementedException();
        }
    }

Last edited Feb 15, 2011 at 1:20 PM by jlevingston, version 3

Comments

No comments yet.