Wcf – Silverlight WCF Service Reference “The given key was not present in the dictionary”

servicesilverlightwcf

Question:

I Have a WCF test Service with a reference (.net 3.5) on Silverlight application and when i make an instance

ServiceReference1.Service1Client client = new Service1Client();

i have the following error:

"The given key was not present in the dictionary."

The service is correct and in web.config i have something like this

<client>
        <endpoint address="http://localhost:49955/Service1.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>
</client>

In a NON silverlight application reference works like a sharm….some ideas to fix it?

PS: the service is a default service:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    void DoWork();
}

public class Service1 : IService1
{
    public void DoWork()
    {
    }
}

MORE INFO

{System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary2.get_Item(TKey key)
at System.ServiceModel.Configuration.ServiceModelSectionGroup.get_Client()
at System.ServiceModel.Description.ConfigLoader.LookupChannel(String configurationName, String contractName, Boolean wildcard)
at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
at System.ServiceModel.ChannelFactory
1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
at System.ServiceModel.EndpointTrait1.CreateSimplexFactory()
at System.ServiceModel.EndpointTrait
1.CreateChannelFactory()
at System.ServiceModel.ClientBase1.CreateChannelFactoryRef(EndpointTrait1 endpointTrait)
at System.ServiceModel.ClientBase1.InitializeChannelFactoryRef()
at System.ServiceModel.ClientBase
1..ctor()
at SilverlightApplication2.ServiceReference1.Service1Client..ctor()
at SilverlightApplication2.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs e)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}

Best Answer

In web.config, which is where the service is supposed to be defined, you have a <system.serviceModel/client> config section - this won't help you define the client from the Silverlight project.

You need a client definition on the SL file ServiceReferences.ClientConfig.

One more thing, SL doesn't support WSHttpBinding, so that client definition which you have will not work for SL.

The ServiceReferences.ClientConfig file should be created when you use the Add Service Reference in your SL project to create a reference to the service. If the service uses a binding which is not supported, then the tool should give you an error / warning.

Related Topic