11,849
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
namespace WCFDataServicesDemoApp.Web
{
public class WebDataService1 :
DataService< /* TODO: put your data source class name here */ >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service
// operations are visible, updatable, etc.
// Examples:
// config.SetEntitySetAccessRule("MyEntityset",
// EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation",
// ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion =
DataServiceProtocolVersion.V2;
}
}
}
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
namespace WCFDataServicesDemoApp.Web
{
public class WCFDataService : DataService<DemoAppEntities>
{
// This method is called only once to
//initialize service-wide policies.
public static void InitializeService(
DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*",
EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion =
DataServiceProtocolVersion.V2;
}
// Define a change interceptor for the Address entity set.
[ChangeInterceptor("Addresses")]
public void OnChangeAddresses(Address address,
UpdateOperations operations)
{
if (operations == UpdateOperations.Add ||
operations == UpdateOperations.Change)
{
if (String.IsNullOrEmpty(address.City) ||
String.IsNullOrEmpty(address.Line1) ||
String.IsNullOrEmpty(address.PostCode))
{
throw new DataServiceException(400,
"Can not save Address with empty City/Line1/PostCode");
}
}
}
}
}
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<service xml:base="http://localhost:1183/WCFDataService.svc/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">
<workspace>
<atom:title>Default</atom:title>
<collection href="Addresses">
<atom:title>Addresses</atom:title>
</collection>
<collection href="Customers">
<atom:title>Customers</atom:title>
</collection>
</workspace>
</service>
private delegate int AddOperationDelegate(int x, int y);
private void button3_Click(object sender, EventArgs e)
{
AddOperationDelegate del = new AddOperationDelegate(add);
lblResult.Text = "Synchronous work Started !";
lblResult.Update(); //This is necessary to repaint the window before calling Thread.Sleep
int res = del.Invoke(10, 20); // or del(10, 20);
MessageBox.Show(string.Format("Result generated : {0}", res));
}
private int add(int x, int y)
{
Thread.Sleep(4000); //Makes the thread to sleep, to make you understand it creates a new thread.
return x + y;
}
private void btnAsynchronous_Click(object sender, EventArgs e)
{
AddOperationDelegate del = new AddOperationDelegate(add);
IAsyncResult res = del.BeginInvoke(10, 20, new AsyncCallback(InvokeMessage), null);
lblResult.Text = "Asynchronous work Started !";
}
private void InvokeMessage(IAsyncResult ar)
{
AddOperationDelegate aod = (ar as AsyncResult).AsyncDelegate as AddOperationDelegate;
int result = aod.EndInvoke(ar);
MessageBox.Show(string.Format("Result generated : {0}", result));
}