Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

WCF Custom names/namespaces/etc in WCF WSDL

by the3factory 4/4/2008 11:37:00 PM
WCF Custom names/namespaces/etc in WCF WSDL
Question:

OK, I'm stupid. I know that. I'm sure the answer to all my questions lies in that great InterWeb thingy. But I can't find them, so I'm asking for help Smile

 

My company is developing a WCF Service, hosted in IIS, to allow integration with another vendor. The vendor uses Java. (What the specifics are of this, I don't know yet.) This service will be depolyed as part of our product, and so we can't hardcode things in the code (like the ServiceContractAttribute or ServiceBehaviorAttribute).

 

This leads to strange things in the generated WSDL, specifically something like:

 

<

Answer1:

You can't access the [ServiceContract] attributes, so you need an entry point to be able to change the data; do you have access to the ServiceHost instance? It should be simple if the service is self-hosted; it's also feasible to do so in webhosted services, but it requires a little more work (http://blogs.msdn.com/carlosfigueira/archive/2007/12/26/modifying-code-only-settings-on-webhosted-services.aspx has information on how to do that).

 

The code below shows how to change all the tempuri.org namespaces in the code. Look that we had to resort to a hack to change the Action property of the operations, as the setter for the property is not public (it's internal), since this is really not a scenario WCF covers (in WCF you're supposed to have control over the [ServiceContract] and friend attributes).

 

public class Post3100698

{

    [DataContract]

    public class MyDC

    {

        [DataMember]

        public string str = "Hello";

    }

    [ServiceContract]

    public interface ITest

    {

        [OperationContract]

        string Echo(string input);

        [OperationContract]

        MyDC EchoDC(MyDC input);

    }

    public class Service : ITest

    {

        public string Echo(string input) { return input; }

        public MyDC EchoDC(MyDC input) { return input; }

    }

    public static void Test()

    {

        ServiceHost host = new ServiceHost(typeof(Service), new Uri("http://localhost:8000/Service"));

        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();

        host.Description.Behaviors.Add(smb);

        smb.HttpGetEnabled = true;

        host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "ITest");

        host.Description.Namespace = "http://my.service.namespace";

        host.Description.Endpoints[0].Binding.Namespace = "http://my.binding.namespace";

        host.Description.Endpoints[0].Contract.Namespace = "http://my.contract.namespace";

        foreach (OperationDescription operation in host.Description.Endpoints[0].Contract.Operations)

        {

            foreach (MessageDescription message in operation.Messages)

            {

                PropertyInfo actionProperty = typeof(MessageDescription).GetProperty("Action", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

                actionProperty.SetValue(message,

                    message.Action.Replace("http://tempuri.org", "http://my.input.namespace"),

                    null);

            }

        }

        host.Open();

        Console.Write("Press ENTER to close service");

        Console.ReadLine();

        host.Close();

    }

}

Answer2:

Thanks a lot. I'll give it a go, and see what I get. (Which means I'll probably be back with more dumb questions.) Regardless, the link you supplied to your blog is fan-freakin-tastic.

 

Thanks a lot,

Chris




MCPD - WinForms
Answer3:

That worked quite well. Thanks a lot for the help.

 

However, one final question if you get a chance. I couldn't figure out how to change the namespace on the following line:

 

<

Tags:

Q&A

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0