I have a simple web application on Asp.net MVC. I created a WFC Service so I can get data to my Windows 8.1 app, but my new functions are not showing in my client side.
This My WFC code:
[DataContract]
public class Service1 : IService1
{
ApplicationDbContext _db=new ApplicationDbContext();
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public WtFImages.Models.Images GetDataL(int value)
{
var User = _db.Image.Local.FirstOrDefault(c => c.Id == value);
return User;
}
public List<WtFImages.Models.Images> GetDataAll(int value)
{
var GetAllPublic = _db.Image.Local.ToList();
return (GetAllPublic);
}
public IList<WtFImages.Models.Images> ZGetDataAll(int value)
{
var GetAllPublic = _db.Image.Local.ToList();
return (GetAllPublic);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
My client side only shows the default functions.
Iservice Code
namespace ImageFechingWfc
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
Each method that you want to expose as service method, should exists in you IService1
interface and should be decorated with [OperationContract]
, then you should implement that method in service class.
IService1.cs
and put signature of your methods is IService1
interface then decorate your new methods with [OperationContract]
, then put implementations in Service1
and rebuild the project, then add your service reference and use it.[DataContract]
above your service implementation.For example if you want to have an int Add(int x, int y)
method in your service, put this in your IService1
interface:
[OperationContract]
int Add(int x, int y);
and then put this in your Service1
class:
public int Add(int x, int y)
{
return x+y;
}
To learn more about WCF services, you can read this Getting Started Tutorials