Silverlight - created a new domainservice but how do I access it from client?
I have used the SL business application template and added a new blank, empty domain service in my Services folder on the .Web part of the solution. The class is DomainService1 and inherits from DomainService. It contains one method:
public class DomainService1 : DomainService
{
public string Hello()
{
return "Hello开发者_StackOverflow社区 World";
}
}
How do I access this service method from the client? I can't seem to create an instance of the domain service at all client side.....
The client side code is generated by RIA Services.
To access services that inherits DomainService you create a new context on the client side.
Replace the "Service" part of the name with "Context".
UserService = UserContext, ArticleService = ArticleContext etc.
Client code
var testContext = new TestContext();
testContext.Hello();
Service code
[EnableClientAccess]
public class TestService : DomainService
{
public string Hello()
{
return "Hello world!";
}
}
Please make sure you have enabled the RIA service for your project.
If your service name ends with a service
tag then you will be able to convert it to a context
like my service name is DomainService1
then at client side it could be accessed by DomainContext1
. If on the server side, my Domainservice name is ABC
, then I can directly access it by name, there is no need to context.
Service code:
[EnableClientAccess]
public class TestService : DomainService
{
public string Hello()
{
return "Hello world!";
}
}
Client code:
On the client side you have to to declare a namespace like system.your web project.web.servicesmodel.client
Now,
TestContext test=new TestContext();
test.Hello(getData,null,false);`
// first parameter is callback method, the second is not important for you, and the third is if any exception occurs then,
public void getData(InvokeOpration<string> value)
{
MessageBox.Show(""+value.Value);
}
Now you can get the Hello World as a MessageBox
.
精彩评论