Freigeben über


Getting Rid of Namespaces

How do I write a contract for a wrapped message in the default namespace?

I've written a quick sample to demonstrate what happens when you write the contract without taking any namespaces into account.

 [ServiceContract]
public interface IService
{
   [OperationContract]
   [WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/")]
   void ProcessRequest(string data);
}

public class Service : IService
{
   public void ProcessRequest(string data)
   {
      Console.WriteLine(OperationContext.Current.RequestContext.RequestMessage);
   }
}

class Program
{
   static void Main(string[] args)
   {
      string address = "localhost:8000/";
      WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(address));
      host.Open();
      ChannelFactory<IService> factory = new ChannelFactory<IService>(new WebHttpBinding());
      factory.Open();
      IService proxy = factory.CreateChannel(new EndpointAddress(address));
      using (new OperationContextScope((IContextChannel)proxy))
      {
         OperationContext.Current.OutgoingMessageHeaders.To = new Uri(address);
         proxy.ProcessRequest("data");
      }
      factory.Close();
      host.Close();
      Console.ReadLine();
   }
}

Running the sample reveals that the message wrapper gets an unpleasant namespace instead of the default namespace.

 <ProcessRequest xmlns="tempuri.org/">
  <data>data</data>
</ProcessRequest>

By looking at the metadata, you could figure out where this namespace was coming from using the custom namespace sample I published earlier. In this case the problem is with the operation wrapper, which comes from the service contract. Setting the service contract to have a namespace of "" gets us the desired default namespace.

Next time: Using Faults with Untyped Messages

Comments

  • Anonymous
    August 11, 2008
    How do I force propagation of changes to information about a certificate revocation list after an update?