8

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...

Dennis Jose
  • 1,589
  • 15
  • 35

2 Answers2

1

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

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

Dennis Jose
  • 1,589
  • 15
  • 35
0

I had the same problem. This post helped me out to do this. You need to write something like this:

SimpleIoc.Default.Register<MyServiceImplementation>(() => {
    return new MyServiceImplementation("Hello World");
});

SimpleIoc.Default.Register<IMyService>(() => {
    return SimpleIoc.Default.GetInstance<MyServiceImplementation>();
});
Community
  • 1
  • 1
Svenisok
  • 446
  • 2
  • 13