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
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...
You can add endpoint address to service client by the following method.
SimpleIoc.Default.Register(() => new Service1Client("WSHttpBinding_IService", wcfConfig.EndpointUrl));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With