ServiceContract attribute inheritance WCF - c#

I am trying to extend a ServiceContract with a callback contract attribute. IProcessSubDataDuplex is generated by a code generator so I need to extend that to integrate CallbackContract attribute. But IProcessDataDuplex does not implement any of the ServiceContract attributes. Is there a workaround for this?
[ServiceContract(CallbackContract = typeof(IProcessDataDuplexCallBack))]
public interface IProcessDataDuplex : IProcessSubDataDuplex
{
}
[ServiceContract(Namespace = "http://www.example.com/", Name = "BeaconMonitorServiceContract", ProtectionLevel = ProtectionLevel.Sign)]
public interface IProcessSubDataDuplex
{
[OperationContract(IsOneWay = true)]
void ProcessData(string rawData);
}

Related

Operation name is not coming in WSDL

I have created a WCF service and my interface looks like below:-
[ServiceContract]
public interface IService1
{
[OperationContract(Action = "GetData")]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
here i am using Action name and when i am generating WSDL from this service i am getting operation name like below :
but when i am using wildcard for action like below :
[ServiceContract]
public interface IService1
{
[OperationContract(Action = "*")]
CompositeType GetDataUsingDataContract(CompositeType composite);
}
here i am not getting operation name in my WSDL.
My question is that how can i use wildcard by generating operation name in WSDL. Please help me to solve this issue or give me suggestions for achieving this.
You can only use [OperationContract(Action = "*")] if your service operation takes a Message object and returns a Message object or void.
See the MSDN documentation for OperationContractAttribute.Action Property

Single WCF Service with Multiple Business Objects Implementaion

Suppose I have 2 interfaces & a single WCF Service ( MyService1 ) in one project:
[ServiceContract(Namespace = "")]
interface IOrders
{
[OperationContract]
void GetOrders();
/*Some more methods here */
}
[ServiceContract(Namespace = "")]
interface ICustomers
{
[OperationContract]
void GetCustomer();
/*Some more methods here */
}
If I add a reference of above project in one of my application (be it a WinForm/Web ),
How Should I structure/Write the MyService1 implementation as so that I can access Members as :
MyService1 svc = new MyService1() ;
svc.Orders.GetOrders();
svc.Customers.GetCustomer();
EDIT:
I tried this :
public class MyService1 : IOrders,ICustomers
{
public void GetOrders()
{
// some thing..
}
public void GetCustomer()
{
}
}
But this does not help as the output for above is :
MyService1 svc = new MyService1() ;
svc.GetOrders();
svc.GetCustomer();

Set response format html Message in WCF C#

There is a WCF-service in C#:
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
class SubscriptionService : ISubscriptionService
{
public Message Help(Message m)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html; charset=utf-8";
return new WebHttpHelpPageMessage();
}
}
[ServiceContract]
interface ISubscriptionService
{
[OperationContract]
[WebInvoke(UriTemplate = "*", Method = "*")]
Message Help(Message m);
}
Message from the inherited class:
public class WebHttpHelpPageMessage : Message
{
...
protected override void OnWriteBodyContents(System.Xml.XmlDictionaryWriter writer)
{
writer.WriteStartElement("html");
writer.WriteRaw("<head></head><body><p>Hi</p></body>");
writer.WriteEndElement();
}
}
The result in the browser - XML, how to do it to be HTML?
since you already have got the xml what i can suggest you to do is create an xml object. And then, create xslt (http://msdn.microsoft.com/en-us/library/ms256069.aspx) as per you desire.
Then you can transform that xml in to HTML using xslt. It's easy that way and you have the control over how you want to display that.
(http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx)
This is good enough if you follow this approach.

Share enum between client and WCF service?

I am having problems sharing my public enum class from my WCF service down to my client program.
(I want to be able to access every enum attributes from my client program). (I have added my service as i Service Reference).
(For testing I only have two EnumMemer - I know..)
I have this in my Service.svc.cs file:
namespace ITHelperService
{
[DataContract]
public class Service : IService
{
[DataMember]
public CommandsEnums comands;
[DataContract(Name="CommandsEnums")]
public enum CommandsEnums
{
[EnumMember]
Get_IPConfig,
[EnumMember]
Get_IPConfig_all,
Get_BIOSVersion,
Get_JavaVersion,
Get_RecentInstalledPrograms,
Get_RecentEvents,
Get_WEIScore,
Do_Ping,
Do_NSLookup
}
}
}
And this is my IService.cs file:
namespace ITHelperService
{
[ServiceContract]
[ServiceKnownType(typeof(ITHelperService.Service.CommandsEnums))]
public interface IService
{
}
}
I have searched the Internet about this problem and it seems that the above should do the trick.
But I can't access them in my client program. It doesn't show up in the intellisense.
Any input please?
I think you are confusing a few things here.
The IService does not have any Operations in it. A ServiceContract should have a few OperationContracts, that you implement in your Service class.
The Implementation of your IService, the Service class, should NOT be a DataContract! It is your implementation of the IService interface.
The Enum CommandsEnums should maybe not be inside the implementation of the Service class, as Simon pointed out.
I would suggest smth like this:
IService.cs file:
namespace ITHelperService
{
[ServiceContract]
[ServiceKnownType(typeof(ITHelperService.Service.CommandsEnums))]
public interface IService
{
[OperationContract]
void Test();
}
}
Service.svc.cs file:
namespace ITHelperService
{
[DataContract]
public class Service : IService
{
public void Test()
{
// This is the method that you can call from your client
}
}
[DataContract(Name="CommandsEnums")]
public enum CommandsEnums
{
[EnumMember]
Get_IPConfig,
[EnumMember]
Get_IPConfig_all,
Get_BIOSVersion,
Get_JavaVersion,
Get_RecentInstalledPrograms,
Get_RecentEvents,
Get_WEIScore,
Do_Ping,
Do_NSLookup
}
}
Your enum shouldn't be included with the server-side code. If you want to share common code, then put it in a common location. That way both the client and server can reference it.

Getting the WCF SessionID in the Client

I am writing a WCF client and am using a ChannelFactory to create my proxy to my Service:
[ServiceContract]
interface Service {
[OperationContract]
void Operation();
}
var proxy = ChannelFactory<MyServiceInterface>.CreateChannel(
new BasicHttpBinding(),
new EndpointAddress("http://localhost:8000/"));
How would I go about getting the SessionID? The proxy only has the basic Object methods as well as the ones defined in MyServiceInterface.
Thank you in advance.
Try this:
In your WCF Service file (.svc) add the following:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
public string SessionId()
{
return OperationContext.Current.SessionId;
}
}
In your WCF Service interface add the following:
[ServiceContract(SessionMode = SessionMode.Required)]
public interface IService1
{
[OperationContract]
string SessionId();
}
In your client, do the following:
ChannelFactory<IService1> factory = new ChannelFactory<IService1>(
new WSHttpBinding(), new EndpointAddress("http://localhost:4213/Service1.svc"));
IService1 proxy = factory.CreateChannel();
Console.WriteLine(proxy.SessionId());
((IClientChannel)proxy).Close();
factory.Close();
Console.Read();
By doing this you can get the proxy session created at service side. Just to know, when ((IClientChannel)proxy).Close(); and factory.Close(); were invoked, then proxy will get new session.
It wasn't what I was looking for to make the SessionId() an OperationContract. If, instead of using the ChannelFactory, I created my own proxy by extending ClientBase<>, and that gave me access to the InnerChannel.SessionId property.

Categories