Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot supply endpoint address while registering WCF service client with SimpleIOC in ViewModel Locator

I’m developing a WPF application with the help MVVM Light Toolkit 4.1.24. Here is my ViewModel Locator class.

public class ViewModelLocator
    {
        /// <summary>
        /// Initializes a new instance of the ViewModelLocator class.
        /// </summary>
        public ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
                // Create design time view services and models
                SimpleIoc.Default.Register<IService1, DesignDataService>();
            }
            else
            {
                // Create run time view services and models
                SimpleIoc.Default.Register<IService1, Service1Client>();
            }

            SimpleIoc.Default.Register<MainViewModel>();
        }

        public MainViewModel Main
        {
            get
            {
                return ServiceLocator.Current.GetInstance<MainViewModel>();
            }
        }

        public static void Cleanup()
        {
            // TODO Clear the ViewModels
            ServiceLocator.Current.GetInstance<MainViewModel>().Cleanup();
        }
    }

Where

  • IService1 - is a WCF service interface

  • DesignDataService – Implementation of IService1 for design purpose

  • Service1Client – WCF proxy class that implement IService1

I have two questions:

1) While run the App, I got an error like this "Cannot register: Multiple constructors found in Service1Client but none marked with PreferredConstructor.". For that I have added "[PreferredConstructorAttribute]" attribute to the Service1Client default constructor and the application run as expected. I know it's not a good method for two reasons

  • it will result a dependency to SimpleIoc
  • Whenever I update service reference I have to manually add this attribute to the default constructor.

So is there any better method?

2) I want to pass the endpoint address to Service1Client manually. How can I do that?

Thanks in advance...

like image 711
Dennis Jose Avatar asked Nov 14 '12 12:11

Dennis Jose


1 Answers

You can add endpoint address to service client by the following method.

SimpleIoc.Default.Register(() => new Service1Client("WSHttpBinding_IService", wcfConfig.EndpointUrl));

like image 52
Dennis Jose Avatar answered Nov 15 '22 06:11

Dennis Jose