1

How do I use WCF without specifying the uri/url/baseaddress? Is there anyway to make it automatically obtain these things?

I mean I wish to make it work like asp.net sites and webservices work where we don't mention the uri and stuff and it takes care of it by itself just as long as we configure it right in IIS. I need a similar solution on WCF.

Please help...

Thank you,.

Edit: Here's my server side code.I'm using a custom ServiceHostFactory here...

 protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {

        BasicHttpBinding basichttpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);

        basichttpbinding.CloseTimeout=TimeSpan.MaxValue;
        basichttpbinding.OpenTimeout=TimeSpan.MaxValue;
        basichttpbinding.ReceiveTimeout=TimeSpan.MaxValue;
        basichttpbinding.SendTimeout=TimeSpan.MaxValue;
       // basichttpbinding.TransferMode = TransferMode.Buffered;

        ServiceEndpoint servicepoint=new ServiceEndpoint(ContractDescription.GetContract(serviceType));
         servicepoint.Binding=basichttpbinding;

        ServiceHost servicehost = new ServiceHost(serviceType, baseAddresses);

        ((ServiceDebugBehavior)servicehost.Description.Behaviors[typeof(ServiceDebugBehavior)]).IncludeExceptionDetailInFaults=true;

        servicehost.OpenTimeout = TimeSpan.MaxValue;
        servicehost.CloseTimeout = TimeSpan.MaxValue;
        servicepoint.Binding.SendTimeout = TimeSpan.MaxValue;
        servicepoint.Binding.ReceiveTimeout = TimeSpan.MaxValue;
        basichttpbinding.MaxBufferPoolSize = 999999999;
       // basichttpbinding.MaxConnections = 999999999;
        //basichttpbinding.MaxConnections = 999999999;
        basichttpbinding.MaxReceivedMessageSize = 999999999;
        XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
        quotas.MaxArrayLength = 999999999;
        quotas.MaxBytesPerRead = 999999999;
        quotas.MaxDepth = 999999999;
        quotas.MaxNameTableCharCount = 999999999;
        quotas.MaxStringContentLength = 999999999;
        basichttpbinding.ReaderQuotas = quotas;

        //foreach (Uri uri in baseAddresses)
        //{

        servicehost.AddServiceEndpoint(typeof(IService), basichttpbinding, "http://localhost:52855/WCFService1/Service.svc");

       // }

        return servicehost;
    }
Josh
  • 13,530
  • 29
  • 114
  • 159
  • 1
    Could you be a bit more specific. What do you mean by 'how do I use WCF' and 'work list asp.net sites' – Ralf de Kleine May 13 '11 at 06:37
  • Specifically without specifying an URI. I need to use without specifying the uri/baseaddress and make it automatically obtain them. – Josh May 13 '11 at 06:58

4 Answers4

3

(Assuming you are targeting IIS...)

According to the Deploying an Internet Information Services-Hosted WCF Service documentation:

... Services hosted in IIS do not have the ability to control their base address; the base address of an IIS-hosted service is the address of its .svc file.

Is it possible that your custom service host logic has stopped this behaviour working?

UPDATE: According to the function docs u can just pass a relative address to the AddServiceEndpoint call. Try either empty string or "/".

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113
  • Im just specifying the endpointaddress and not the base address.I wish I don't have to specify any address at all. If I compile the code without using any address, it compiles good but throws an error at runtime asking for an endpoint address. – Josh May 13 '11 at 13:00
  • I'm not sure you read my answer properly. The WCF docs claim the base address is supplied automatically and is "the address of the .svc file"... so in your example that should be http://localhost:52855/WCFService1/Service.svc – Jack Ukleja May 13 '11 at 13:17
  • OK I can see you are passing the base addresses in.... in that case you dont need to provide an absolute address for the endpoints, just pass a relative address – Jack Ukleja May 13 '11 at 13:22
2

Have a look at WCF Discovery it allows you to dynamically find endpoints.

Emond
  • 50,210
  • 11
  • 84
  • 115
  • WCF discovery does not let me stay without specifying a uri on the server side. It's for the clients to find the server. I don't mind providing the uri for the clients. My problem is the server..why does the server need to know where it's hosted? I want it to automatically find it...Thanks.. – Josh May 13 '11 at 07:05
  • 1
    It doesn't. Show me where in the server side code the url is specified. – Emond May 13 '11 at 07:21
  • I've posted my code. Tell me how i can change this code so that I don't have to specify any url. Thanks. – Josh May 13 '11 at 12:10
  • Have a look at this: http://geekswithblogs.net/dlanorok/archive/2007/07/18/Dynamic-Configuration-for-WCF-Service-Base-Address.aspx This way you can specify the base address in a configuration file. If you do not want that, how would you want to specify and know where your service is listening (what address, what port)? – Emond May 13 '11 at 12:29
  • Also, see this: http://stackoverflow.com/questions/56249/wcf-service-configuration-file-question-regarding-baseaddresses – Emond May 13 '11 at 12:29
  • BTW: if you are so worried about hard-coding values, why are you hard-coding all your configuration? – Emond May 13 '11 at 12:30
2

I think all you need to do to your code is this:

servicehost.AddServiceEndpoint(typeof(IService), basichttpbinding, "");

to accomplish what you want. As @Schneider pointed out, IIS controls assignment of the service address. The endpoint address will always be relative to the IIS assign address. So the only valid value for an IIS based endpoint would something like:

servicehost.AddServiceEndpoint(typeof(IService), basichttpbinding, "MyService");

Which will append MyService to the service URL.

Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
0

I had a similar problem - this is what fixed it for me:

Add this to the top of your interface:

[ServiceContract(Namespace = "your namespace here")]

In your app.config, make sure your service name in the section is in the form Namespace.Class

Hope this helps.

Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
user1229458
  • 253
  • 1
  • 5
  • 10