Accessing my own datalayer using RIA & Silverlight - silverlight

Is it possible for me to connect to my own data provide via WCF RIA services?
I've created a small datalayer that connnects to DynamicAX datasource. I would like to use Silverlight 4 & RIA service to access that datalayer.
At it's most basic -I've done the following...
1) I've added an empty domainclass to the webproject and in that class I created a simple method to return a string...
[EnableClientAccess()]
public class ProjectService : DomainService
{
public string TestViaRIA()
{
return "Hello!";
}
}
2) I then added reference to the web project in my silvelight class and created a bit of code to try and invoke the method...
using ProjectApp.Web;
namespace ProjectApp.Views
{
public partial class ProjectControl : UserControl
{
public ProjectControl()
{
InitializeComponent();
ProjectContext ctx = new ProjectContext();
var x = ctx.TestViaRIA();
testTextBox.Text = x.ToString();
}
}
}
the returned value is "{System.ServiceModel.DomainServices.Client.InvokeOperation}".
I'm obviously doing something wrong here and I would appreciate some guidance on how I can achive this.

Add [Invoke] attribute on the method

Related

WCF sending information to WPF application

I want to send data from WCF to my desktop application while executing a service method.
The WCF service is hosted in windows service.
For example: In my service I am counting from 1 to 100 and when I am hitting 10,20,30 etc I want to set that value on my textbox.
This may be done using duplex channels services:
http://msdn.microsoft.com/en-us/library/ms731064.aspx
Be careful however : this will not work if the server can't reach the client : for example if you are behind a proxy.
I've simpy passed that with get/set like below: (Maybe someone will search smthing similar in future)
public class CallbackHandler : WCFService.IWCFServiceCallback
{
public ListBox LtBox { get; set; }
public void Message(string result)
{
LtBox.Items.Add(result);
}
}
InstanceContext instanceContext = new InstanceContext(new CallbackHandler() { LtBox=this.ltBox });
Where ltBox is my Listbox in desktop application.
Thanks all for help.

How do I call custom methods in a Wcf Data Service from a Silverlight application?

I have the following Wcf Data Service:
public class WcfDataService : DataService<WcfDataServiceContext>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.UseVerboseErrors = true;
}
[WebGet]
public IQueryable<Person> GetPeopleByName(string name)
{
WcfDataServiceContext context = this.CurrentDataSource;
var match = from p in context.People
where p.FirstName == name
select p;
return match;
}
I can access the custom method from the browser like this:
http://127.0.0.1:8080/DataService/WcfDataService.svc/GetPeopleByName?name='Daniel'
How can I call that method and get that list of Person from a Silverlight application?
I'm using Visual Studio 2012, Silverlight 5, .NET Framework 4.0.
As far as I remember whe using Silverlight you cannot connect to a different server than the one the Silverlight app came from so you would just use a relative Uri. If you would like to use WCF Data Services client you can take a look here: http://forums.silverlight.net/t/208481.aspx - there is a code snippet that shows it. However AFAIK WCF Data Services client does not support Service Operation so you may need to use XmlReader to be able to query and parse the response of the GetPeopleByName function.
I did it before I can share.Service Reference your domain:8080/DataService/WcfDataService.svc then
For Person object use [DataContract] attribute for properties of Peson use [global::System.Runtime.Serialization.DataMemberAttribute()] By this way you say Serialize and create proxies to Bus side. Notice this attributes because it really works!
//Here is the interface attributes are important
namespace AHBSBus.Web.Services
{
[ServiceContract]
public interface IChatService
{
[OperationContract]
bool LogIn(Guid userID,Guid roomID);
[OperationContract]
bool LogOut(Guid userID,Guid roomID);
[OperationContract]
IEnumerable<VW_CHATUSERDETAIL> GetLatestMessages(Guid userID,Guid roomID,Guid lastSyncMessageID);
[OperationContract]
bool SendMessage(Guid fromID, Guid roomID, Guid toID, string message);
[OperationContract]
IEnumerable<ChatUser> GetLoggedInUsers(Guid roomID);
[OperationContract]
bool IsLogin(Guid roomID,Guid userID);
}
}
//Implementation of service
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public partial class ChatService:IChatService
{
//Here goes
}

How to initialize local fields of a WCF proxy class on deserialization

In my Silverlight client I have a partial class created by setting a WCF reference. I've extended this class adding a few RelayCommand properties. I need to initialize these properties which I would normally do in the constructor. However it seems that the constructor is not being called, which I believe is a result of of VTS However I'm also unsuccessful in using the OnDeserialized attribute.
What is the prescribed way to initialize client side data members of a WCF class.
I've created a sample project and everything works as expected. If this code doesn't help - post your data contract and client code.
namespace SilverlightApplication3.ServiceReference1
{
public partial class SomeModel
{
public string ExtendedProperty { get; set; }
[OnDeserializing]
public void OnDeserializingMethod(StreamingContext context)
{
this.ExtendedProperty = "Ok";
}
}
}
Service method call:
var proxy = new ServiceReference1.Service1Client();
proxy.DoWorkCompleted += (s,e) => Debug.WriteLine(e.Result.ExtendedProperty); //Ok
proxy.DoWorkAsync();

Unable to return collections or arrays from JAX-WS Web Service

I found that I was unable to return collections from my JAX-WS Web Service.
I appreciate that the Java Collections API may not be supported by all clients, so I switched to return an array, but I can't seem to do this either.
I've set up my web service as follows:
#WebService
public class MyClass {
public ReturnClass[] getArrayOfStuff() {
// extremely complex business logic... or not
return new ReturnClass[] {new ReturnClass(), new ReturnClass()};
}
}
And the ReturnClass is just a POJO. I created another method that returns a single instance, and that works. It just seems to be a problem when I use collections/arrays.
When I deploy the service, I get the following exception when I use it:
javax.xml.bind.MarshalException - with linked exception:
[javax.xml.bind.JAXBException: [LReturnClass; is not known to this context]
Do I need to annotate the ReturnClass class somehow to make JAX-WS aware of it?
Or have I done something else wrong?
I am unsure of wheter this is the correct way to do it, but in one case where I wanted to return a collection I wrapped the collection inside another class:
#WebService
public class MyClass {
public CollectionOfStuff getArrayOfStuff() {
return new CollectionOfStuff(new ReturnClass(), new ReturnClass());
}
}
And then:
public class CollectionOfStuff {
// Stuff here
private List<ReturnClass> = new ArrayList<ReturnClass>();
public CollectionOfStuff(ReturnClass... args) {
// ...
}
}
Disclaimer: I don't have the actual code in front of me, so I guess my example lacks some annotations or the like, but that's the gist of it.

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

Resources