`
huhu_long
  • 浏览: 71763 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

MCTS Self-Paced Training Kit (Exam 70-513 WCF)

    博客分类:
  • .net
阅读更多
Serialize order for data members of the data contract
1. members without explicitly set the order value, and the order is the same with the alpha of each data member, for example:
[DataMember(IsRequired = false]
public string Country;

should before

[DataMember(IsRequired = false]
public string Name;


2. members with order value, sort with the order value ASC, same order value order by alpha of the data member, same with #1

3. member without [DataMember] tag won't be serialized.

-------------------------------------------------- I'm the splitter ------------------------------------------
[MessageContract(IsWrapped=true)]
MessageContractAttribute can be applied to class and structure to define message structure. The default value of IsWrapped property is true. So in default, a SOAP message will use class name as the tag and all data members will be associated with it as the children tags. For instance
[DataContract]
public class TestClass
{
    [DataMember]
    public string Name;
}

then the SOAP likes
...
<TestClass>
    <Name>...</Name>
</TestClass>
...

if IsWrapped = false, SOAP likes
...
<Name>...</Name>
...


-------------------------------------------------- I'm the splitter ------------------------------------------

Three elements constitute and endpoint - ABCs of endpoints
A - Address, which is a URL identities the location of the service. An address contains fours parts, Scheme(http://, net.tcp://, this is not the same thing with protocal), Machine(www.google.com or localhost), Port(this is optional, preceded by a colon:), Path(this is possible when a service resides in a directory structure, e.g. webservice application hosted by IIS, http://localhost:8081/TestServiceApp/AccountService)
B - Binding, which determines how the service can be accessed. Such as the protocol used to access the service, the encoding method used to format the message contents, specify security requirement like SSL or SOAP message security. BTW, a service can have multiple endpoints, which means you can try different bindings and the client can use the best one available.
C - Contract, the fully qualified type name of the contract, e.g. MyNamespace.MyService

-------------------------------------------------- I'm the splitter ------------------------------------------

When sending a request to service, the dispatcher on the server will responsible for dispatching incoming request and call service type instance.
So if the operation is a One-Way operation, which means the dispatcher only need to dispatch this request, no need wait the response.
But if the request with large message and therefore time-consuming, it can invoke the operation asynchronously.

The conclusion is: it is make sense to invoke a One-Way operation asynchronously.

-------------------------------------------------- I'm the splitter ------------------------------------------

Begin_ & End_
As you know, if you select "Generate asynchronous operations" option when adding web service reference to project, we will get Begin_ & End_ operation pair together with operation itself. How to use it?

Binding binding = new WSHttpBinding();
ChannelFactory factory = new ChannelFactory(binding, "http://localhost:8080/MyService.svc");

IMyServiceChannel proxy = factory.CreateChannel();

proxy.BeginMyOpertion(123, new AsyncCallBack(MyCallBack), proxy);
// Other code logic...


private void MyCallBack(IAsynResult result)
{
    IMyServiceChannel proxy = result.AsyncState as IMyServiceChannel;
    string value = proxy.EndMyOperation(result);
    // Other code logic...
}


-------------------------------------------------- I'm the splitter ------------------------------------------
3 ways to create proxy instance

    class Program
    {
        static void Main(string[] args)
        {
            // 1: Look for the endpoint configuration name to create proxy instance.
            Service1Client sc = new Service1Client("WSHttpBinding_IService1");
            string resultValue = sc.GetData(123);

            // 2: Use endpoint uri and binding to create proxy instance.
            EndpointAddress ea = new EndpointAddress("http://localhost:45504/Service1.svc");
            Service1Client codeProxy = new Service1Client(new WSHttpBinding(), ea);
            string value = codeProxy.GetData(123);

            // 3: Use channel factory to create dynamic proxy instance via binding and endpoint uri
            ChannelFactory<IService1Channel> factory =
                new ChannelFactory<IService1Channel>(new WSHttpBinding(), "http://localhost:45504/Service1.svc");

            IService1Channel proxy = factory.CreateChannel();
            proxy.BeginGetData(123, new AsyncCallback(HandleCallBack), proxy);

            System.Console.ReadLine();
        }

        static void HandleCallBack(IAsyncResult result)
        {
            IService1Channel client = result.AsyncState as IService1Channel;

            string value = client.EndGetData(result);
            System.Console.WriteLine("done");
        }
    }


-------------------------------------------------- I'm the splitter ------------------------------------------
4 steps to use tracing in WCF
- Config WCF to generate trace data
- Set trace level
- Config trace listener (e.g. where to put the log? c:\aaa.svclog)
- Enable message loging
-------------------------------------------------- I'm the splitter ------------------------------------------
When the quota for logging messages is reached, WCF generates one additional message to indicate that the quota has been reached, so the actual number of log entries will be the specified quota plus 1. For example:
<system.serviceModel>
    <diagnostics>
        <messageLogging logentrieMessage="true"
                        logMalformedMessage="false"
                        logMessagesAtServiceLevel="true"
                        logMessagesAtTransportLevel="false"
                        maxMessagesToLog="1000"
                        maxSizeOfMessageToLog="2000"
    </diagnostics>
</system.serviceModel>
So, according to the desc above, the maximum number of WCF messages that will appear in the trace log should be 1000 + 1 = 1001
-------------------------------------------------- I'm the splitter ------------------------------------------
Adding Message Inspectors to the Pipeline
1. Create Behavior
    a) public class YourCustomBehavior : IEndpointBehavior
    b) create inspector, assume your inspector named "TestInspector", which implements both "IClientInspector" and "IDispatchInspector" interfaces
    c) add inspector in methods "ApplyClientBehavior" and "ApplyDispatchBehavior" in YourCustomBehavior class, like below:
// to add inspection to the client side.
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
    TestInspector inspector = new TestInspector();
    clientRuntime.MessageInspectors.Add(inspector);
}

// to add inspection to the server side.
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
    TestInspector inspector = new TestInspector();
    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
}

    d) now you have a behavior, it needs to be added to a behavior extension. Like below, the behavior extension extends from BehaviorExtensionElement class
public class YourTestBehaviorExtensionElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        return typeof(YourCustomBehavior);
    }

    public override object CreateBehavior
    {
        return new YourCustomBehavior();
    }
}

    e) refine your config file
<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="YourCustomBehavior">
                [b]<messageLogger />[/b]
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <extensions>
        <behaviorExtensions>
            <add name="messageLogger" type="your.assembly.namespace.typename, assembly.name, version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </behaviorExtensions>
    </extensions>
</system.serviceModel>


P.S, there is alternative way to apply customer behavior to pipeline
ServiceProxy proxy = new ServiceProxy();
proxy.Endpoint.Behaviors.Add(new YourCustomBehavior());

-------------------------------------------------- I'm the splitter ------------------------------------------
Transport-Level Security
Below bindings are support transport-level security
  • basicHttpBinding
  • basicHttpContextBinding
  • wsHttpBinding
  • netTcpBinding
  • netTcpContextBinding
  • netNamedPipeBing
  • msmqIntegrationBinding
  • netMsmqBinding


Message-Level Security
Below bindings are support message-level security
  • basicHttpBinding
  • basicHttpContextBinding
  • wsHttpBinding
  • netTcpBinding
  • netTcpContextBinding
  • netMsmqBinding
  • wsDualHttpBinding


-------------------------------------------------- I'm the splitter ------------------------------------------
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics