How to create a URI template in WCF? - c#

I'm new to wcf services development. I have the following problem:
I'm creating wcf-service with URI template like this:
[OperationContract, WebGet(UriTemplate = "/EmpDetails/command/?command=SaveDetails&id={id}&data{empid:{EmpID},EmpName:{EmpName},EmpAge:{EmpAge}}"
How can I access these values to save details?
another thing is i want this URL to be used to save details.
http://12.154.21.23:8888/EmpDetails/command/?command=SaveDetails&data={empid:Test,EmpName:TestName,EmpAge:26}

You need to create a class somewhere in service:
[DataContract]
public class Data
{
[DataMember]
public int EmpID {get;set;}
[DataMember]
public string EmpName{get;set;}
[DataMember]
public string EmpAge {get;set;}
}
Next, add this to your wcf service interface:
[OperationContract]
[WebGet(UriTemplate = "/EmpDetails/command/?command=SaveDetails&id={id}&data{EmpID:{EmpID},EmpName:{EmpName},EmpAge:{EmpAge}}")]
void SaveDetails(int id, Data data);
and finally, add code below to class implementing wcf service interface:
public void SaveDetails(int id, Data data)
{
//do smt
}

Related

How to call WCF service from MVC web api and bind model?

Hi,
How to call WCF Service from MVC web api and bind the service model into MVC model? Can you please help ?????
[DataContract]
public class Employee
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int Age { get; set; }
}
This is the employee class
EndpointAddress endpointAddress = new EndpointAddress(ConfigurationManager.AppSettings["ServiceKey"]);
string endpointConfigurationName = "SampleSerVice";
ClassProxy obj = new ClassProxy(endpointConfigurationName, endpointAddress);
var result = obj.GetEmployeeName();
result returns the list of employee, but how to bind this to MVC Model
First of all you need to create WCF Service.
After that you will had to add the ServiceBase Reference in your project.
and then go to the link which is provided by the WCFService.
and then Add the Service by giving Proper name.
After that you need to add the endpoints of the WCF Service which you can find in the Appconfig in the Project where you have added the ServiceBase Reference.
Copy the binding name=" " and End points from the AppConfig and paste it in the Web Config.
You can Access the WCF Service and to use the Service You need to create the instance of it and then whatever models or methods you need to access it.

ServiceStack: JsonServiceClient usage without IReturn in DTO

What I would like to do is the following:
var client = new JsonServiceClient(ServiceUrl);
var request = new FooQuery {Id = 1};
IEnumerable<Project> response = client.Get(request);
However, my FooQuery doesn't implement any IReturn, and I'd like it not to (it's in a library without ServiceStack references). Here's my service side:
Library of business objects:
public class ProjectQuery
{
public int Id { get; set; }
}
AppHost:
Routes.Add<ProjectQuery>("/project", "GET");
Service:
public object Get(Foo request)
{
// do stuff.
}
Is there some nice, clean way to create the JsonServiceClient without using the IReturn interface on my business object?
Looks like there's no way not to use IReturn if you don't want to provide a URL to the JsonServiceClient Get() requests. Just decided to create another set of DTOs in my ServiceStack implementation, that are essentially mirrors of the real DTOs in another library. Then when a request comes in to my SS DTO, I create the other library's DTO, set each property, and pass it along.
Not pretty, but that's the best I could find so far.
I had the same problem using IReturn and Routes, as I wanted to use the DTOs
in assemblies with business logic, without ServiceStack references.
It worked for me, using in the Client Model
public class TestRequest
{
public int vendorId {get; set; }
public string barcode {get; set; }
public string username { get; set; }
public string password { get; set; }
}
then in the AppHost
Routes.Add<TestRequest( "/TestAPI/Reservation/{vendorId}/{barcode}"," GET,OPTIONS")
.Add<TestRequest>("/TestAPI/Reservation", "POST, OPTIONS")
and the call for JsonServiceClient with POST
request.vendorId=12344;
request.barcode="AAS1223";
TestResponse response = client.Post<TestResponse>(server_ip + "/TestAPI/Reservation", request);
OR with GET
TestResponse response = client.Get<TestResponse>(server_ip + "/TestAPI/Reservation/12344/AAS1223?username=John&password=99");
Then in the service Get or Post functions
public TestResponse Get(TestRequest request)
{
// request members hold the values of the url.
return DoBusinessLayerWork(request);
}
Using the Send() method from the JsonServiceClient type is the way to go about doing this.

How to get the request object from wcf message?

I have a WCf service with Contracts shown below.
[MessageContract]
public class ServiceRequest
{
[MessageBodyMember]
public int RequestId { get; set; }
[MessageBodyMember]
public OrderDetails OrderDetails { get; set; }
}
[DataContract]
public class OrderDetails
{
[IsLogRequired]
public int OrderId { get; set; }
[IsLogRequired]
public int Quantity { get; set; }
public string CustomerName { get; set; }
}
[IsLogRequired] is custom Attribute.
We need to get all properties in the request which have "[IsLogRequired]" attribute when the request is received. We want to do it as generic solution so that it can be plugged into all services.
We thought of using "MessageInspector" to do this implementing "IDispatchMessageInspector".
How do i get the actual request object from "System.ServiceModel.Channels.Message" parameter of IDispatchMessageInspector.AfterReceiveRequest() method?
Please correct me if i am using a wrong interface or wrong method. Any other solution to this?
I am assuming that "[IsLogRequired] is custom property." means a custom attribute...
Simple answer is that there is no solution to transfer custom attributes that are decorating the data contract as you described it.
Data contracts should be pure and not encumbered by business logic. The know how about the what should be done with various fields belongs to a service implementation.
Possible approach could look like this:
public class OrderService : IOrderService
{
private void ProcessOrder(Order order)
{
var ra = new AuditMetadataResourceAccess();
MethodInfo[] fieldsToLog = ra.GetLoggingFields(typeof(OrderDetal));
if (fieldsToLog.Any())
{
var logger = new LogingEngine();
logger.Log(fieldsToLog, order.OrderDetails);
}
}
}
You could move this implementation inside message inspector or operation invoker. Carlos Figueira has extensive description of each WCF extensibility point.
"How do i get the actual request object from "System.ServiceModel.Channels.Message" parameter of IDispatchMessageInspector.AfterReceiveRequest() method?"
I am assuming you are referring to Web request. WebOperationContext.Current but you need to have ASP.NET Compatibility Mode turned on.

Consuming RIA Service. Not updating in Silverlight client?

I made some changes to my interface for a RIA service, but SL is not getting them when I do an update.
Basically changed a few from void to bool so I could check for completion.
I've tried deleting, getting again, etc.
No matter what I do, when I call my client side context it still shows the old interface.
[ServiceContract]
public interface IUploadService
{
[OperationContract]
bool UploadFile(CrmFileUpload fileUpload);
}
public partial class CrmFileUpload
{
public string FileName { get; set; }
public byte[] Chunk { get; set; }
}
Updated code:
public void TestMe(string testString)
{
_context.TestMethodAsync(testString); //this shows void, but everything in the WCF is a string
}

WCF REST Starter Kit not filling base class members on POST

I have a WCF REST Starter Kit service. The type handled by the service is a subclass of a base class. For POST requests, the base class members are not correctly populated.
The class hierarchy looks like this:
[DataContract]
public class BaseTreeItem
{
[DataMember]
public String Id { get; set; }
[DataMember]
public String Description { get; set; }
}
[DataContract]
public class Discipline : BaseTreeItem
{
...
}
The service definition looks like:
[WebHelp(Comment = "Retrieve a Discipline")]
[WebGet(UriTemplate = "discipline?id={id}")]
[OperationContract]
public Discipline getDiscipline(String id)
{
...
}
[WebHelp(Comment = "Create/Update/Delete a Discipline")]
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "discipline")]
public WCF_Result DisciplineMaintenance(Discipline discipline)
{
...
}
Problem: While the GET works fine (returns the base class Id and Description), the POST does not populate Id and Description even though the XML contains the fields.
Sample XML:
<?xml version="1.0" encoding="utf-8"?>
<Discipline xmlns="http://schemas.datacontract.org/2004/07/xxx.yyy.zzz">
<DeleteFlag>7</DeleteFlag>
<Description>2</Description>
<Id>5</Id>
<DisciplineName>1</DisciplineName>
<DisciplineOwnerId>4</DisciplineOwnerId>
<DisciplineOwnerLoginName>3</DisciplineOwnerLoginName>
</Discipline>
Thanks for any assistance.
I could not solve the problem using a DataContractSerializer. I switched to using the XMLSerializerFormat and everything worked fine. In fact, the capabilities of the XMLSerializer are so much better that for purely XML work, it is probably better to use the XMLSerializer in all cases.

Categories