DataContract not working and classes are not showing on the client - wcf

[ServiceContract(SessionMode = SessionMode.Required)]
public interface IActionService
{
[OperationContract]
MovieResponse GetReviews(MovieRequest request);
[OperationContract]
UserResponse GetUsers(UserRequest request);
[OperationContract]
UserResponse InsertUser(UserRequest request);
}
[DataContract]
public class UserRequest
{
[DataMember]
public int userId;
[DataMember]
public User user;
}
I cant see the userrequest and userresponse in my ServiceReference at the client. Why?

I had a reference to the WCF Service application assembly which should not be there. After removing the reference I could see my Classes.
Thanks

Related

How to Write a WCF service method which should accept the logs as a collection in C#?

How to Write a WCF service method which should accept the logs as a collection in C# ?
My WCF service should accept the collection of log messages and then we have to insert into the DB.
Thanks !!!
You can implement that simply as follows: ( you might add yourself any other parameters you want to the service method including service headers)
[DataContract]
public class LogData
{
[DataMember]
public long LogId { get; set; }
[DataMember]
public string LogMessage { get; set; }
}
[ServiceContract]
public interface ILoggerService
{
[OperationContract]
void SaveLogs(List<LogData> logs);
}
public class LoggerService : ILoggerService
{
public void SaveLogs(List<LogData> logs)
{
// write your DB specific logic to loop through the logs object and store into the db.
}
}

The underlying connection was closed in WCF with Serializers

I have a WCF service with a service interface
[ServiceContract]
public interface IMyService
{
[OperationContract]
[ServiceKnownType(typeof(Person))]
[ServiceKnownType(typeof(Employee))]
IPerson GetPerson();
}
and my Implementation of GetPerson is
public IPerson GetPerson()
{
IPerson obj = new Person();
obj.FirstName = "Bhuvan";
obj.LastName = "Ram";
return obj;
}
And in my client as simple I used
KnownType.MyServiceClient obj = new KnownType.MyServiceClient();
Person objp = (Person)obj.GetPerson()'
But when I am trying to access, I am receiving an error
The underlying connection was closed: The connection was closed unexpectedly.
and my stack trace is like this
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)..
My Employee Class is
>
[DataContract(Name = "Employee")]
[KnownType(typeof(IEmployee))]
public class Employee:IEmployee
{
[DataMember]
public string EmployeeName
{
get;set;
}
}
>
My Person Class is
[DataContract(Name = "Person")]
[KnownType(typeof(IPerson))]
public class Person: IPerson
{
#region IPerson Members
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
#endregion
}
How do I resolve this?
There might be a serialization issue on Concrete class Person.
[ServiceContract]
public interface IMyService
{
[OperationContract]
IPerson GetPerson();
}
public interface IPerson
{
string FirstName { get; set; }
string LastName { get; set; }
}
[DataContract(Name = "Person")]
[KnownType(typeof(IPerson))]
public class Person : IPerson
{
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
}
Can use ServiceKnownType attribute on the method signature, rather than using the KnownType attribute on the whole class
[OperationContract]
[ServiceKnownType(typeof(Person))]
IPerson GetPerson();
Source: Check this link for detail
Ref : Using Knowntype
Note:
Use Knowntype everywhere or ServiceKnowntype on ServiceContract only.
Try with unique namespace to DataContract of Person and Employee classes.

WCF Interface as parameter

I am using interface as input parameter in OperationContract. But when i generate proxy class at client side. I am not able to access the members of interface or class implemeting the ITransaction interface. I am only geeting is object
Service Interface
[ServiceContract]
public interface IServiceInterface
{
[OperationContract]
string SyncDatabase(ITransaction TransactionObject);
}
Service class
class SyncService:IServiceInterface
{
public string SyncDatabase(ITransaction TransactionObject)
{
return "Hello There!!";
}
}
Interface
public interface ITransaction
{
ExpenseData ExpData { get; set; }
void Add(ITransaction transactionObject);
}
Data Contract
[DataContract]
public class Transaction:ITransaction
{
[DataMember]
public ExpenseData ExpData
{
get;
set;
}
public void Add(ITransaction transactionObject)
{
}
}
In above case should i also copy the iTransaction class and interface on client
You actually need to make your ServiceContract aware of the implementation of the interface you pass as a parameter, so WCF will include it in the WSDL.
This should work:
[ServiceContract]
[ServiceKnownType(typeof(Transaction))]
public interface IServiceInterface
{
[OperationContract]
string SyncDatabase(ITransaction TransactionObject);
}
Use [KnownType(typeof(testClass))].
Refer these links:
msdn.microsoft.com/en-us/library/ms730167.aspx
www.codeproject.com/Tips/108807/Implementing-KnownType-Attribute
Try making your interface the [DataContract] and use the [KnownType] attribute to tell WCF what the known implementations of that interface are.
[DataContract]
[KnownType(typeof(Transaction))]
public interface ITransaction
{
[DataMember]
ExpenseData ExpData { get; set; }
void Add(ITransaction transactionObject);
}

WebGet and object as GET parameter in WCF REST client

Let's say we have REST client with next declaration:
[ServiceContract]
interface ITestClient
{
[OperationContract]
[WebGet(UriTemplate = "SetData/?d1={d1}&d2={d2}")]
void SetData(string d1, string d2);
}
I would like tu use it with next signature (HTTP GET):
[ServiceContract]
interface ITestClient
{
[OperationContract]
[WebGet(UriTemplate = "SetData/?")]
void SetData(SetDataRequest setData);
}
[DataContract]
public class SetDataRequest
{
[DataMember(Name = "d1")]
private string Data1 { get; set; }
[DataMember(Name = "d2")]
private string Data2 { get; set; }
}
I wish that WCF serializes instance of the SetDataRequest to HTTP QueryString.
Is this possible (HTTP POST is not acceptable)?
You should not use WebGet for this you should instead do:-
[OperationContract]
[WebInvoke( UriTemplate="SetDate/" Method="POST")]
void SetData(SetDataRequest setData);
You should use a post when submitting data as you will run into security/caching issues if you don't.

WCF service contract and data contract

I have following code
[ServiceContract(Namespace = "http://www.myweb.com/prod")]
public interface IBaseService
{
[OperationContract]
public string GetName(IDMessageContract ID)
}
[ServiceContract(Namespace = "http://www.myweb.com/prod/child")]
public interface IChildService : IBaseService
{}
public class BaseService
{ public string GetName(IDMessageContract ID)}
public class ChildService: IChildService
{}
[MessageContract]
public class IDMessageContract
{
public string ID{get;set;}
}
In above scenario I need the GetName method SOAP header containing the namespace "http://www.myweb.com/prod/child"
If you need SOAP header with specified namespace you must specify that header in message contract and use its Namespace property. Something like:
[MessageContract]
public class IDMessageContract
{
[MessageHeader(Namespace="http://www.myweb.com/prod/child")]
public string MyHeader { get; set;}
[MessageBodyMember]
public string ID{get;set;}
}