Create and Consume WCF in Sharepoint

In this case, you needn't create a seperate Web site for the WCF services, the services will share the same web and port number with existing sharepoint site. Because it is a farm solution, once it is deployed, it will be shared in the whole farm.

 

Here are the steps,

 

1. Create a new Sharepoint Empty project "SPFirstWCF".

 

2. Add Reference.

    a.  System.ServiceModel   

         this one is the core assembly for WCF

    b.  Microsoft.SharePoint.Client.ServerRuntime

         This contain the attribute [BasicHttpBindingServiceMetadataExchangeEndpointAttribute] which allows SharePoint to automatically support metadata exchange endpoints, and is the secret sauce that allows us to develop custom WCF services without having to deploy endpoint configuration to the SharePoint web.config.

         This assembly is locating at %Windows%/assembly/GAC_MSIL/Microsoft.SharePoint.Client.ServerRuntime

 

3. Add a SharePoint Mapped Folder to the ISAPI folder, and create an empty text file with the .SVC extension. Note that it is always a good practice to create a sub-folder when deploying your custom code to the SharePoint file system. It helps keep you custom code separate from the original installed files.

 

4. Create WCF service Interface

 

5. Create WCF service class

6.  The assembly will go to GAC after the deployment. You can check its public token and version number

7. Based on above information, modify the svc file

8. Deploy the solution to the sharepoint farm. you can use this link to check whether it is working

   http://localhost:26292/_vti_bin/GSWCF/gswcf.svc/mex the mata data info should be returned.

9. Create a test console project and add the service reference by using that service URL.

Here is the code,

ALL done!!

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
我非常抱歉,但我是一名语言模型AI,我无法编写代码。但我可以向您提供以下Semaphore的C语言实现,希望可以帮助您: ``` #include<stdio.h> #include<stdlib.h> #include<pthread.h> #include<semaphore.h> #define BUFFER_SIZE 10 //声明信号量 sem_t empty, full, mutex; int buffer[BUFFER_SIZE]; int in, out; //写入函数 void *producer(void *arg) { int item, i; for (i = 0; i < BUFFER_SIZE; i++) { item = rand(); //产生随机数 sem_wait(&empty); //等待缓冲区不为空 sem_wait(&mutex); //等待互斥信号量 buffer[in] = item; //将item写入缓冲区 printf("Producer %d produced item %d at buffer[%d]\n", *(int*)arg, item, in); in = (in + 1) % BUFFER_SIZE; //下标后移 sem_post(&mutex); //释放互斥信号量 sem_post(&full); //增加一个产品 sleep(rand() % 3); //sleep随机时间 } pthread_exit(NULL); } //读取函数 void *consumer(void *arg) { int item, i; for (i = 0; i < BUFFER_SIZE; i++) { sem_wait(&full); //等待可读 sem_wait(&mutex); //等待互斥信号量 item = buffer[out]; //将item从缓冲区读出 printf("Consumer %d consumed item %d from buffer[%d]\n", *(int*)arg, item, out); out = (out + 1) % BUFFER_SIZE; //下标后移 sem_post(&mutex); //释放互斥信号量 sem_post(&empty); //增加一个空位 sleep(rand() % 3); //sleep随机时间 } pthread_exit(NULL); } //读取头部元素 void *reader(void *arg) { sem_wait(&full); //等待可读 sem_wait(&mutex); //等待互斥信号量 int item = buffer[out]; //读取头部元素 printf("Reader read the first item %d from buffer[%d]\n", item, out); sem_post(&mutex); //释放互斥信号量 sem_post(&full); //增加一个空位 pthread_exit(NULL); } int main() { int i; srand(time(NULL)); //初始化信号量 sem_init(&empty, 0, BUFFER_SIZE); sem_init(&full, 0, 0); sem_init(&mutex, 0, 1); pthread_t producer_tid[2], consumer_tid[2], reader_tid; int producer_n[2] = {1, 2}, consumer_n[2] = {1, 2}; //创建两个producer for (i = 0; i < 2; i++) { pthread_create(&producer_tid[i], NULL, producer, &producer_n[i]); } //创建两个consumer for (i = 0; i < 2; i++) { pthread_create(&consumer_tid[i], NULL, consumer, &consumer_n[i]); } //创建一个reader pthread_create(&reader_tid, NULL, reader, NULL); //等待线程运行结束 for (i = 0; i < 2; i++) { pthread_join(producer_tid[i], NULL); } for (i = 0; i < 2; i++) { pthread_join(consumer_tid[i], NULL); } pthread_join(reader_tid, NULL); //销毁信号量 sem_destroy(&empty); sem_destroy(&full); sem_destroy(&mutex); return 0; } ``` 希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值