Register WCF / REST

I was wondering if anyone could show me how to log a simple request / response from my wcf rest service.

I myself am hosting with a console application on localmachine:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(RawDataService), new Uri(baseAddress));
            WebHttpBinding binding = new WebHttpBinding();
            //binding.Security.Mode = WebHttpSecurityMode.Transport;
            host.AddServiceEndpoint(typeof(IReceiveData), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
            host.Open();
            Console.WriteLine("Host opened");
            Console.ReadLine();
        }
    }
}

I really hoped that all that was needed was something added to the console hosting application. I tried to follow this, but it was abit confusing http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx

Just to note that I do not use the app.config or web.config files.

EDIT:

I also can not use third-party products for this.

+3
source share
2 answers

?

, WCF. , , Windows SDK - , , , .Net Windows SDK...

http://msdn.microsoft.com/en-us/library/ms733025.aspx

0

- () Dynamic Proxy Castle. , /- , , . , , "" - , , , . , :

public class LoggingInterceptor : IInterceptor
{
    // No matter what service method is called, it funneled through here.
    public void Intercept(IInvocation call)
    {
        MyLogger.Info("Starting call: " + call.Method.Name);

        // Actually invoke whatever method was originally called 
        call.Proceed();

        MyLogger.Info("Finished call: " + call.Method.Name);
    }
}

, . , :

using Castle.DynamicProxy;
...

// Create your service object and then create a dynamic proxy of the object
// that will inject your logging interceptor logic.
ProxyGenerator generator = new ProxyGenerator();
RawDataService service = new RawDataService();
RawDataService proxy = generator.CreateClassProxyWithTarget<RawDataService>(
    service,
    new LoggingInterceptor());

// Register your proxy object, not the raw service w/ WCF
WebServiceHost host = new WebServiceHost(proxy, new Uri(baseAddress));
... rest of your code as it was ...

, RawDataService, Intercept(), Proceed(), . , , StopWatch log, , .

. "" IoC /- , , . AOP-:

0

All Articles