How to bind WPF control to WCF method using ObjectDataProvider
I am testing WPF for the first time and I am trying to call a WCF service using ObjectDataProvider.
WCF service named WcfService1 with one method:
namespace WcfService1
{
public class Service1 : IService1
{
public String HelloWorld()
{
return "Hello World!";
}
}
}
I added a service reference to my WPF project and named it TestService
In my main window, I can call this with no problem in code. It sounds simple; like calling a web service:
TestService.Service1Client service = new TestService.Service1Client (); MessageBox.Show (service.HelloWorld ());
I am trying to create an ObjectDataProvider that points to this service. I guess I'm confused about what the ObjectType should be? I've tried local, service namespace, src; I am lost:
<Window.Resource>
<ObjectDataProvider
x:Key="odpTestService"
ObjectType="{x:Type **TestService**:Service1Client}"
MethodName="HelloWorld" />
</Window.Resources>
Ultimately it binds to the TextBlock:
<TextBlock Grid.Column="0" Grid.Row="0"
Grid.ColumnSpan="2" Background="AliceBlue"
Text="{Binding Source={StaticResource odpTestService}}" />
I tried to work from the Flickr example posted here: http://khason.net/blog/wpf-binding-to-wcf-and-more/
Update: The answer from Denis resolved part of the question here. Now I get a compile error: System.Windows.Data error: 34: ObjectDataProvider: error when trying to call a method by type;
ObjectDataProvider cannot call HelloWorld method with type IService1 (using method and type from my example). Any ideas why?
source to share
You need to import the service namespace via the xmlns directive at the top of the file:
Assuming the reference was added directly to your application and that your root application namespace is "MyApplication":
<Window x:class="MyApplication.MyWindow"
xmlns:srv="MyApplication.TestService">
<Window.Resource>
<ObjectDataProvider
x:Key="odpTestService"
ObjectType="{x:Type srv:Service1Client}"
MethodName="HelloWorld" />
</Window.Resources>
<TextBlock Grid.Column="0" Grid.Row="0"
Grid.ColumnSpan="2" Background="AliceBlue"
Text="{Binding Source={StaticResource odpTestService}}" />
</Window>
source to share