3

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

Calanus
  • 25,619
  • 25
  • 85
  • 120

2 Answers2

4

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!";
        }
    }
Einarsson
  • 522
  • 2
  • 14
0

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.

jonsca
  • 10,218
  • 26
  • 54
  • 62
Dhaval Patel
  • 7,471
  • 6
  • 37
  • 70