Entity Framework GET Web Service Throwing Errors - c#

I have this GREAT WCF service that returns Data from EF.
public class HistoryDataService : DataService<HistoryEntities>
{
#region Public Methods
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
[WebGet]
public IQueryable<History> GetHistoriesById(int recordId)
{
return CurrentDataSource.Histories.Where(d => d.RecordId == recordId);
}
#endregion
}
I have other services that I added something like:
[WebGet(UriTemplate = "eventdetails/{id}", ResponseFormat = WebMessageFormat.Json)]
to make it more of a traditional RESTful service, however, when I add it I get various errors like:
... both defines a ServiceContract and inherits a ServiceContract from type System.Data.Services.IRequestHandler.`
How do I add this property or is it even possible?

It would save you a lot of time to use WCF Data Services or OData as it is called. You'll get both JSON and XML output for your web service response. Your choice. : )
WCF Data Services

Related

WCF Services: Requests are limited to a single "Request" Paramater

I have a very strange and obscure issue with WCF services that I was hoping to get some insight on:
I am working a WCF service that we are building to replace one that we no longer have source code for. For some reason, in the new WCF service, everything is forced through a single paramater called "request". Using the WCF test client, this is what it looks like
On the "correct" service, this is what it looks like:
Is there any reason why this would be happening? I've defined all of the requests as follows:
[ServiceContract]
public interface IMyService
{
[OperationContract]
string SomeRequest();
}
Which seems correct, but there may be something I've overlooked that is causing this.
In your original WCF service, there is a request function parameter, and it has a definition similar to the following:
[ServiceContract]
public interface IMyService
{
[OperationContract]
Request SomeRequest(Request request);
}
[DataContract]
public class Request
{
string documentId;
[DataMember]
public string DocumentId
{
get { return documentId; }
set { documentId = value; }
}
}
In the new wcf service:
[ServiceContract]
public interface IMyService
{
[OperationContract]
string SomeRequest(string documentId);
}
So this is because the function parameters are different. Originally your parameter was class, but later changed to string, so the display in WCFTestClient is different.

how can i consume a method from a connected service in .NET WCF and expose it 'AS IS' in a new method in my WCF

My interface look like this:
[ServiceContract]
public interface IMyService
{
[OperationContract]
myConnectedService.SomeComplexResponseType someMethod(myConnectedService.SomeComplexRequestType request);
}
My implementation look like this:
public class MyService : IMyService
{
myConnectedService_client client = new myConnectedService_client();
public myConnectedService.SomeComplexResponseType someMethod(myConnectedService.SomeComplexRequestType request)
{
myConnectedService.SomeComplexResponseType response = client.connectedServiceMethod(request);
return response ;
}
}
The error i get when i am trying to run my service:
Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.
and
error CS0644: 'System.ComponentModel.PropertyChangedEventHandler' cannot derive from special class 'System.MulticastDelegate'

OperationContext.Current is null in .net core

I'm providing a WCF service in my .net core project via SoapCore. In one of the methods, I want to access the SOAP envelope header info. To test that, I've added the following code:
[ServiceContract]
public interface IDataService {
[OperationContract]
string TransmitObject(XElement node);
}
// -------------------------------------------------------------------------
public string TransmitObject(XElement node) {
foreach(var header in OperationContext.Current.IncomingMessageHeaders) {
Console.WriteLine(header.ToString());
}
return JsonConvert.SerializeXNode(node, Formatting.None);
}
However, in this case, the OperationContext.Current attribute is always null. What do I need to change in order to make that work?

Check if your are in wcf service

I use a WCF service and wonder if I can use the OperationContract methods for the caller and for the service.
Therefore I'd like to know the best way to say if the code is running in the application or in the service.
Like this:
[ServiceContract]
public interface IService
{
[OperationContract]
bool ServiceMethod(string param);
}
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single,
InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext=false)]
public class Service : IService
{
bool ServiceMethod(string param)
{
if(!isInWcfService) //How to do this?
{
//Call this ServiceMethod in WCF Service
}
else
{
//Do the work
}
}
}
Since the calling program and the service knows this class, I think it might be easier if both just have to call this one method and it decides itself if it has to forward the call to the service or can just do to work.
Thank you!
You can check if you are inside a WCF service by checking OperationContext.Current, which is a WCF service class comparable to HttpContext.Current in ASP.NET:
if (OperationContext.Current != null)
{
// inside WCF
}
else
{
// not
}

ASMX Web Service Soap Extension - How to Inject Attribute into Client Proxy Class?

I try set soap extension attributes on client side. For example:
Implementation in web service:
[AttributeUsage(AttributeTargets.Method)]
public class EncryptMessageAttribute : SoapExtensionAttribute
{
private string strKey="null";
public string StrKey
{
get { return strKey; }
set { strKey = value; }
}
}
Soap extension class:
public class EncryptMessage : SoapExtension
{
...
}
Used on web method:
[WebMethod]
[EncryptMessage( StrKey = "pass")]
public string test2()
{
return "ok";
}
Implementation in Proxy class:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/test", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[EncryptMessage( StrKey = "pass")]
public string test() {
object[] results = this.Invoke("test", new object[0]);
return ((string)(results[0]));
}
Soap extension attributes are::[EncryptMessage( StrKey = "pass")]
I want to set Soap Extension Attribute on client side, before than I use Soap Extension, when I call some web methods.
Example: I call some method, wich set soap extension attributes on both side, before than soap extension is used. Can somebody help me ?
First of all, if you can use WCF for this, then you should. Microsoft has stated that ASMX web services are "legacy technology", and that all new web service development should use WCF.
In any case, see the SoapExtensionReflector and SoapExtensionImporter classes. Note that these will only work for .NET, ASMX clients.

Categories