开发者

WCF Client is unable to see endpoint but WCF Client can connect to base address

I get the following error when i try to consume my wcf service

Could not find endpoint element with name 'http://localhost:8080/Provider/basic' and contract 'Provider.IProvider' in the ServiceModel client configuration section

I can however connect to my base address through the WCF Test Client on http://localhost:8080/Provider

I've tried updating my service reference and that didn't work, Anyone know what is wrong with my setup

    public ServiceHost ProviderServiceHost { get; set; }

    private void StartProvider()
    {
        if (ProviderServiceHost != null)
            Abort();
        ProviderServiceHost = new ServiceHost(typeof(Provider), new Uri("http://localhost:8080/Provider"));
        var binding = new BasicHttpBinding
        {
            Name = "basicBinding",
            HostNameComparisonMode = HostNameComparisonMode.WeakWildcard,
            Security = { Mode = BasicHttpSecurityMode.None }
        };

        var metadataBehavior = ProviderServiceHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
        if (metadataBehavior == null)
        {
            metadataBehavior = new ServiceMetadataBehavior { HttpGetEnabled = true };
            ProviderServiceHost.Descr开发者_开发百科iption.Behaviors.Add(metadataBehavior);
        }

        ProviderServiceHost.AddServiceEndpoint(typeof(IProvider), binding, "http://localhost:8080/Provider/basic");

        ProviderServiceHost.Open();
    }

My client is connecting like this

  private static ProviderClient _proxy = new ProviderClient(http://localhost:8080/Provider/basic);

If i don't put in an address then i get this exception

Message "Could not find default endpoint element that references contract 'Provider.IProvider' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element." string


Are you using client side configuration? As it appears you're using the ClientBase proxy I expect you've used 'Add Service Reference...' and are just the default configuration file.

The overload that you're using is new ServiceClient(string endpointConfigurationName) - the string value represents a Name, not an Address. If you check your configuration file you should see a client section has been added:

<client>
    <endpoint address="http://localhost:8080/Provider/basic" 
              binding="basicHttpBinding"
              bindingConfiguration="basicBinding_IProvider" 
              contract="ServiceReference1.ITest"
              name="basicBinding_IProvider" />
</client>

So if you change your Client constructor to use this Name property it should work better for you.

private static ProviderClient _proxy = 
    new ProviderClient("basicBinding_IProvider");

On the other hand if you are not using a configuration file and want to specify the address in code, you can use a different ClientBase constructor:

using System.ServiceModel;
// ...

_client = new ProviderClient(new BasicHttpBinding(), 
            new EndpointAddress("http://localhost:8080/Provider/basic"));

There are a number of different overloads in the ClientBase class (your ProviderClient) which allow you to specify various properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜