如何编写Web服务:处理外部事件 [英] How to write web service : To handle external events

查看:46
本文介绍了如何编写Web服务:处理外部事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了UCMA控制台应用程序。它向多个用户发送IM。


现在假设发生了一些外部事件,然后我的ucma应用程序应该向用户发送IM。我有sahrepoint网站 外部事件可能是数据库中的任何更改。 


所以我的要求是我需要在UCMA应用程序之上开发Web服务。


<我完全是新手。


在网上我发现创建网络服务。在Web服务中编写实现。创建控制台应用程序并在其中添加Web服务的引用。我指的是以下链接 


http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html  


我的UCMA代码如下


 


使用系统;


使用System.Collections.Generic;


使用System.Linq;


使用System.Text;


使用System.Configuration;


使用System.Security.Principal;


使用System.Security.Cryptography.X509Certificates;


使用System.Net;


使用System.Threading;


使用System.Diagnostics;


使用Microsoft.Rtc.Collaboration;


使用Microsoft.Rtc.Signaling;


使用System.Runtime。 InteropServices;


//使用Microsoft.Rtc.Collaboration.Sample.Common;


命名空间UserPresence


{


  &NBSP; class UCMASampleInstantMessagingCall


  &NBSP; {


  &NBSP; &NBSP; &NBSP; #region当地人


  &NBSP; &NBSP; &NBSP; //对话和远端参与者的信息。 &NBSP; &NBSP; &NBSP;  


 


  &NBSP; &NBSP; &NBSP; //谈话的主题;将出现在 


 的标题栏的中心。 &NBSP; &NBSP; &NBSP; //如果Microsoft Lync是远端客户端,则为对话窗口。


  &NBSP; &NBSP; &NBSP; private static String _conversationSubject =" Microsoft Lync Server!" ;;


 


  &NBSP; &NBSP; &NBSP; //对话的优先级将显示在


 的标题栏的左下角&NBSP; &NBSP; &NBSP; //如果Microsoft Lync是远端客户端,则为对话窗口。


  &NBSP; &NBSP; &NBSP; private static String _conversationPriority = ConversationPriority.Urgent;


 


  &NBSP; &NBSP; &NBSP; //将发送到远端的即时消息。


  &NBSP; &NBSP; &NBSP; private static String _messageToSend =" Hello World!我是一个机器人,无论你打字什么都会回应。 " +


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; "请发送'bye'以结束此申请。" ;;


 


  &NBSP; &NBSP; &NBSP; private InstantMessagingCall _instantMessaging;


 


  &NBSP; &NBSP; &NBSP; private InstantMessagingFlow _instantMessagingFlow;


 


  &NBSP; &NBSP; &NBSP; // private ApplicationEndpoint _applicationEndpoint;


  &NBSP; &NBSP; &NBSP;私人UserEndpoint _userendpoint;


  &NBSP; &NBSP; &NBSP;私人UCMASampleHelper _helper;


 


  &NBSP; &NBSP; &NBSP; //完成样本后通知应用程序主线程的事件。


  &NBSP; &NBSP; &NBSP; private AutoResetEvent _sampleCompletedEvent = new AutoResetEvent(false);


  &NBSP; &NBSP; &NBSP; #endregion


  &NBSP; &NBSP; &NBSP; #region方法


  &NBSP; &NBSP; &NBSP; ///< summary>


  &NBSP; &NBSP; &NBSP; ///实例化并运行InstantMessagingCall快速入门。


  &NBSP; &NBSP; &NBSP; ///< / summary>


  &NBSP; &NBSP; &NBSP; ///< param name =" args"> unused< / param>


  &NBSP; &NBSP; &NBSP; // private string _helper;


  &NBSP; &NBSP; &NBSP; // UCMASampleHelper _helper = new UCMASampleHelper();


  &NBSP; &NBSP; &NBSP; public static void Main(string [] args)


  &NBSP; &NBSP; &NBSP; {


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; UCMASampleInstantMessagingCall ucmaSampleInstantMessagingCall =


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;新的
UCMASampleInstantMessagingCall();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ucmaSampleInstantMessagingCall.Run();


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; private void Run()


  &NBSP; &NBSP; &NBSP; {


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //初始化并启动平台。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;例外ex = null;


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //创建UserEndpoint


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _helper = new UCMASampleHelper();


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _userendpoint = _helper.CreateEstablishedUserEndpoint();


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.Write("URI所拥有的用户端点:");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.Write(_userendpoint.OwnerUri);


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("现已建立并注册。");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;列表与LT;串GT; _Users = new List< string>();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _Users.Add(" sip:demouser@lync.abc.com");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _Users.Add(" sip:demouser1@lync.abc.com");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; foreach(_Users中的字符串useruri)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //设置对话并拨打电话。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ConversationSettings convSettings = new ConversationSettings();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; convSettings.Priority = _conversationPriority;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; convSettings.Subject = _conversationSubject;


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //对话代表一系列交流模式


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //(媒体类型)在一个或多个对话框的上下文中


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // callees。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // Convers conver = new ConversationParticipant


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;会话对话=新对话(_userendpoint,convSettings);


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; InstantMessagingCall _instantMessaging = new InstantMessagingCall(对话);


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //调用:StateChanged:仅用于记录日志。通常, 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //这可用于表示调用状态对UI的更改


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessaging.StateChanged + = this.InstantMessagingCall_StateChanged;


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //订阅流程创建的事件;该流程将用于


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //发送媒体(此处为IM)。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //最终,作为回调的一部分,消息将是 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //发送/接收。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessaging.InstantMessagingFlowConfigurationRequested + =


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.InstantMessagingCall_FlowConfigurationRequested;


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //获取与之通信的远端用户的SIP地址。


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // String _calledParty =" sip:" +


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //  &NBSP; UCMASampleHelper.PromptUser(


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //&NBSP;&NBSP;"输入登录到微软的用户的URI的Lync,在用户@主机格式=>",


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; //&NBSP;&NBSP;" RemoteUserURI" ;);


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //将呼叫发送到远程方,而不指定任何 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //自定义选项。请注意对话主题 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //覆盖toast消息,所以如果你想看到祝酒词


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //消息,请将会话主题设置为空。


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // _instantMessaging.Conversation()


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessaging.BeginEstablish(useruri,新ToastMessage(QUOT;你好吐司"),NULL,


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;  CallEstablishCompleted,_instantMessaging);


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; catch(InvalidOperationException iOpEx)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //无效操作如果提供的数据可能会抛出异常


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //对BeginXXX方法无效/格式错误。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):在这里写下实际的处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ex = iOpEx;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;终于


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(ex!= null)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //如果操作引发异常,请终止该示例,


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //并将例外打印到控制台。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):在这里写下实际的处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(ex.ToString());


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("由于错误导致关闭平台");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _helper.ShutdownPlatform();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //等待样本完成


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _sampleCompletedEvent.WaitOne();


 


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; //只需在控制台中记录状态转换。


  &NBSP; &NBSP; &NBSP; void InstantMessagingCall_StateChanged(object sender,CallStateChangedEventArgs e)


  &NBSP; &NBSP; &NBSP;   {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(QUOT;呼叫的状态发生变化之前的呼叫状态是:" + e.PreviousState +


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;"当前状态为:" + e.State);


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; //创建的流程表示存在流程以开始媒体 


  &NBSP; &NBSP; &NBSP; //使用的操作,并且它不再为空。


  &NBSP; &NBSP; &NBSP;公共无效InstantMessagingCall_FlowConfigurationRequested(对象发件人,


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; InstantMessagingFlowConfigurationRequestedEventArgs E)


&NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("Flow Created。");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow = e.Flow;


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //现在流程非空,将事件处理程序绑定到状态 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //已更改并收到消息。当流量激活时,


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //(如状态更改事件所示)该程序将发送 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //事件处理程序中的IM。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.StateChanged + = this.InstantMessagingFlow_StateChanged;


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //收到的消息是用于表示消息有


的事件  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //从远端收到。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.MessageReceived + = this.InstantMessagingFlow_MessageReceived;


&NBSP;


&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //此外,这是一个绑定到 


的好地方  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // InstantMessagingFlow.RemoteComposingStateChanged要接收的事件


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //输入远端用户的通知。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.RemoteComposingStateChanged + =


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; this.InstantMessagingFlow_RemoteComposingStateChanged;


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; private void InstantMessagingFlow_StateChanged(object sender,MediaFlowStateChangedEventArgs e)


  &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(QUOT + e.PreviousState + QUOT;流动状态从"改变成QUOT; + e.State);


&NBSP;


&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //当流量处于活动状态时,媒体操作(此处,发送IM) 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //可以开始。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(e.State == MediaFlowState.Active)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //在InstantMessagingFlow上发送消息。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.BeginSendInstantMessage(_messageToSend,SendMessageCompleted,


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; _instantMessagingFlow);


&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP;私人无效InstantMessagingFlow_RemoteComposingStateChanged(对象发件人,


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; ComposingStateChangedEventArgs $ b $是)


  &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //打印远端用户的输入通知。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(QUOT;参与者QUOT;


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP; + e.Participant.Uri.ToString()


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP + QUOT;是"


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP; + e.ComposingState.ToString()


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;  );


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; private void InstantMessagingFlow_MessageReceived(object sender,InstantMessageReceivedEventArgs e)


  &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //在传入的即时消息上,将内容打印到控制台。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(e.Sender.Uri +" say:" + e.TextBody);


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //如果远端告诉我们,请关机。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(e.TextBody.Equals(" bye",StringComparison.OrdinalIgnoreCase))


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //关闭平台将终止所有附加的对象。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //如果这是一个生产应用程序,它会拆除 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //呼叫/对话,而不是终止整个平台。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.BeginSendInstantMessage(QUOT;关闭..." ;, SendMessageCompleted,


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; _instantMessagingFlow);


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _helper.ShutdownPlatform();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _sampleCompletedEvent.Set();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;否则


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //将即时消息回送到远端(发送者 


         //即时消息)。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //在向远端发送邮件时更改本地最终用户的撰写状态。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //故意引入延迟以演示由 


显示的输入通知  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //远端客户端;否则通知的持续时间不会太长。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.LocalComposingState = ComposingState.Composing;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Thread.Sleep(2000);


 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //使用"Echo"回显消息前缀。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.BeginSendInstantMessage(QUOT;回声:" + e.TextBody,SendMessageCompleted,


&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; _instantMessagingFlow) ;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; private void CallEstablishCompleted(IAsyncResult result)


  &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; InstantMessagingCall instantMessagingCall = result.AsyncState as InstantMessagingCall;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;例外ex = null;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; instantMessagingCall.EndEstablish(result);


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("呼叫现在处于已建立状态。");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; catch(OperationFailureException opFailEx)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // OperationFailureException:表示无法连接 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //致电远程派对。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):写出真正的错误处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ex = opFailEx;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; catch(RealTimeException rte)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //其他错误可能会导致抛出其他RealTimeExceptions。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):写出真正的错误处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ex = rte;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;终于


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(ex!= null)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //如果操作引发异常,请终止该示例,


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //并将例外打印到控制台。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):写出真正的错误处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(ex.ToString());


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("由于错误导致关闭平台");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _helper.ShutdownPlatform();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; }


 


  &NBSP; &NBSP; &NBSP; private void SendMessageCompleted(IAsyncResult result)


  &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; InstantMessagingFlow instantMessagingFlow = result.AsyncState as InstantMessagingFlow;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;例外ex = null;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;试试


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; instantMessagingFlow.EndSendInstantMessage(result);


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("邮件已发送。");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; catch(OperationTimeoutException opTimeEx)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // OperationFailureException:表示无法连接 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //由于超时而向远程方发送IM(被叫方未能


         //在预期时间内回复) 。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):写出真正的错误处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ex = opTimeEx;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; catch(RealTimeException rte)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //其他错误可能会导致抛出其他RealTimeExceptions。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):写出真正的错误处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; ex = rte;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;终于


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //重置本地最终用户的撰写状态,以便输入所见的通知 


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //由远端客户端消失。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _instantMessagingFlow.LocalComposingState = ComposingState.Idle;


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(ex!= null)


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //如果操作引发异常,请终止该示例,


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //并将例外打印到控制台。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; // TODO(留给读者):写出真正的错误处理代码。


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine(ex.ToString());


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.WriteLine("由于错误导致关闭平台");


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; _helper.ShutdownPlatform();


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; #endregion


  &NBSP; }


}

我有平台和端点创建的帮助文件

解决方案

您是否查看过UCMA 3.0 SDK附带的Contact Center参考应用程序?


I have developed UCMA console application . It sends IM to multiple users .

Now Suppose Some external event happen then my ucma application should send IM to users . I have sahrepoint site External event may be any changes in database . 

So My requirment is I need to develop web service above UCMA application.

I am totally new to this.

On net I found that Create web service . Write implementation in web service . create console application and add reference of web service in that .I am referring following link 

http://sarangasl.blogspot.com/2010/09/create-simple-web-service-in-visual.html 

My UCMA code is as follows

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Configuration;

using System.Security.Principal;

using System.Security.Cryptography.X509Certificates;

using System.Net;

using System.Threading;

using System.Diagnostics;

using Microsoft.Rtc.Collaboration;

using Microsoft.Rtc.Signaling;

using System.Runtime.InteropServices;

//using Microsoft.Rtc.Collaboration.Sample.Common;

namespace UserPresence

{

    class UCMASampleInstantMessagingCall

    {

        #region Locals

        // The information for the conversation and the far end participant.        

 

        // Subject of the conversation; will appear in the center of the title bar of the 

        // conversation window if Microsoft Lync is the far end client.

        private static String _conversationSubject = "The Microsoft Lync Server!";

 

        // Priority of the conversation will appear in the left corner of the title bar of the

        // conversation window if Microsoft Lync is the far end client.

        private static String _conversationPriority = ConversationPriority.Urgent;

 

        // The Instant Message that will be sent to the far end.

        private static String _messageToSend = "Hello World! I am a bot, and will echo whatever you type. " +

            "Please send 'bye' to end this application.";

 

        private InstantMessagingCall _instantMessaging;

 

        private InstantMessagingFlow _instantMessagingFlow;

 

        //private ApplicationEndpoint _applicationEndpoint;

        private UserEndpoint _userendpoint;

        private UCMASampleHelper _helper;

 

        // Event to notify application main thread on completion of the sample.

        private AutoResetEvent _sampleCompletedEvent = new AutoResetEvent(false);

        #endregion

        #region Methods

        /// <summary>

        /// Instantiate and run the InstantMessagingCall quickstart.

        /// </summary>

        /// <param name="args">unused</param>

        //private string _helper;

        //UCMASampleHelper _helper = new UCMASampleHelper();

        public static void Main(string[] args)

        {

 

            UCMASampleInstantMessagingCall ucmaSampleInstantMessagingCall =

                                                                        new UCMASampleInstantMessagingCall();

            ucmaSampleInstantMessagingCall.Run();

        }

 

        private void Run()

        {

 

            // Initialize and startup the platform.

            Exception ex = null;

 

            try

            {

 

                // Create the UserEndpoint

                _helper = new UCMASampleHelper();

 

                _userendpoint = _helper.CreateEstablishedUserEndpoint();

 

                Console.Write("The User Endpoint owned by URI: ");

                Console.Write(_userendpoint.OwnerUri);

                Console.WriteLine(" is now established and registered.");

                List<string> _Users = new List<string>();

                _Users.Add("sip:demouser@lync.abc.com");

                _Users.Add("sip:demouser1@lync.abc.com");

                foreach (string useruri in _Users)

                {

                // Setup the conversation and place the call.

                ConversationSettings convSettings = new ConversationSettings();

                convSettings.Priority = _conversationPriority;

                convSettings.Subject = _conversationSubject;

 

                // Conversation represents a collection of modes of communication

                // (media types)in the context of a dialog with one or multiple 

                // callees.

                //Convers conver =new ConversationParticipant

                Conversation conversation = new Conversation(_userendpoint, convSettings);

                InstantMessagingCall _instantMessaging = new InstantMessagingCall(conversation);

 

                // Call: StateChanged: Only hooked up for logging. Generally, 

                // this can be used to surface changes in Call state to the UI

                _instantMessaging.StateChanged += this.InstantMessagingCall_StateChanged;

 

                // Subscribe for the flow created event; the flow will be used to

                // send the media (here, IM).

                // Ultimately, as a part of the callback, the messages will be 

                // sent/received.

                _instantMessaging.InstantMessagingFlowConfigurationRequested +=

                    this.InstantMessagingCall_FlowConfigurationRequested;

 

                // Get the sip address of the far end user to communicate with.

 

                //String _calledParty = "sip:" +

                //    UCMASampleHelper.PromptUser(

                //    "Enter the URI of the user logged onto Microsoft Lync, in the User@Host format => ",

                //    "RemoteUserURI");

 

                // Place the call to the remote party, without specifying any 

                // custom options. Please note that the conversation subject 

                // overrides the toast message, so if you want to see the toast 

                // message, please set the conversation subject to null.

 

                    // _instantMessaging.Conversation()

 

                    _instantMessaging.BeginEstablish(useruri, new ToastMessage("Hello Toast"), null,

                        CallEstablishCompleted, _instantMessaging);

                }

            }

            catch (InvalidOperationException iOpEx)

            {

                // Invalid Operation Exception may be thrown if the data provided

                // to the BeginXXX methods was invalid/malformed.

                // TODO (Left to the reader): Write actual handling code here.

                ex = iOpEx;

            }

            finally

            {

                if (ex != null)

                {

                    // If the action threw an exception, terminate the sample, 

                    // and print the exception to the console.

                    // TODO (Left to the reader): Write actual handling code here.

                    Console.WriteLine(ex.ToString());

                    Console.WriteLine("Shutting down platform due to error");

                    _helper.ShutdownPlatform();

                }

            }

 

            // Wait for sample to complete

            _sampleCompletedEvent.WaitOne();

 

        }

 

        // Just to record the state transitions in the console.

        void InstantMessagingCall_StateChanged(object sender, CallStateChangedEventArgs e)

         {

            Console.WriteLine("Call has changed state. The previous call state was: " + e.PreviousState +

                "and the current state is: " + e.State);

        }

 

        // Flow created indicates that there is a flow present to begin media 

        // operations with, and that it is no longer null.

        public void InstantMessagingCall_FlowConfigurationRequested(object sender,

            InstantMessagingFlowConfigurationRequestedEventArgs e)

        {

            Console.WriteLine("Flow Created.");

            _instantMessagingFlow = e.Flow;

 

            // Now that the flow is non-null, bind the event handlers for State 

            // Changed and Message Received. When the flow goes active, 

            // (as indicated by the state changed event) the program will send 

            // the IM in the event handler.

            _instantMessagingFlow.StateChanged += this.InstantMessagingFlow_StateChanged;

 

            // Message Received is the event used to indicate that a message has

            // been received from the far end.

            _instantMessagingFlow.MessageReceived += this.InstantMessagingFlow_MessageReceived;

 

            // Also, here is a good place to bind to the 

            // InstantMessagingFlow.RemoteComposingStateChanged event to receive

            // typing notifications of the far end user.

            _instantMessagingFlow.RemoteComposingStateChanged +=

                                                    this.InstantMessagingFlow_RemoteComposingStateChanged;

        }

 

        private void InstantMessagingFlow_StateChanged(object sender, MediaFlowStateChangedEventArgs e)

        {

            Console.WriteLine("Flow state changed from " + e.PreviousState + " to " + e.State);

 

            // When flow is active, media operations (here, sending an IM) 

            // may begin.

            if (e.State == MediaFlowState.Active)

            {

                // Send the message on the InstantMessagingFlow.

                _instantMessagingFlow.BeginSendInstantMessage(_messageToSend, SendMessageCompleted,

                    _instantMessagingFlow);

            }

        }

 

        private void InstantMessagingFlow_RemoteComposingStateChanged(object sender,

                                                                        ComposingStateChangedEventArgs e)

        {

            // Prints the typing notifications of the far end user.

            Console.WriteLine("Participant "

                                + e.Participant.Uri.ToString()

                                + " is "

                                + e.ComposingState.ToString()

                                );

        }

 

        private void InstantMessagingFlow_MessageReceived(object sender, InstantMessageReceivedEventArgs e)

        {

            // On an incoming Instant Message, print the contents to the console.

            Console.WriteLine(e.Sender.Uri + " said: " + e.TextBody);

 

            // Shutdown if the far end tells us to.

            if (e.TextBody.Equals("bye", StringComparison.OrdinalIgnoreCase))

            {

                // Shutting down the platform will terminate all attached objects.

                // If this was a production application, it would tear down the 

                // Call/Conversation, rather than terminating the entire platform.

                _instantMessagingFlow.BeginSendInstantMessage("Shutting Down...", SendMessageCompleted,

                    _instantMessagingFlow);

                _helper.ShutdownPlatform();

                _sampleCompletedEvent.Set();

            }

            else

            {

                // Echo the instant message back to the far end (the sender of 

                // the instant message).

                // Change the composing state of the local end user while sending messages to the far end.

                // A delay is introduced purposely to demonstrate the typing notification displayed by the 

                // far end client; otherwise the notification will not last long enough to notice.

                _instantMessagingFlow.LocalComposingState = ComposingState.Composing;

                Thread.Sleep(2000);

 

                //Echo the message with an "Echo" prefix.

                _instantMessagingFlow.BeginSendInstantMessage("Echo: " + e.TextBody, SendMessageCompleted,

                    _instantMessagingFlow);

            }

 

        }

 

        private void CallEstablishCompleted(IAsyncResult result)

        {

            InstantMessagingCall instantMessagingCall = result.AsyncState as InstantMessagingCall;

            Exception ex = null;

            try

            {

                instantMessagingCall.EndEstablish(result);

                Console.WriteLine("The call is now in the established state.");

            }

            catch (OperationFailureException opFailEx)

            {

                // OperationFailureException: Indicates failure to connect the 

                // call to the remote party.

                // TODO (Left to the reader): Write real error handling code.

                ex = opFailEx;

            }

            catch (RealTimeException rte)

            {

                // Other errors may cause other RealTimeExceptions to be thrown.

                // TODO (Left to the reader): Write real error handling code.

                ex = rte;

            }

            finally

            {

                if (ex != null)

                {

                    // If the action threw an exception, terminate the sample, 

                    // and print the exception to the console.

                    // TODO (Left to the reader): Write real error handling code.

                    Console.WriteLine(ex.ToString());

                    Console.WriteLine("Shutting down platform due to error");

                    _helper.ShutdownPlatform();

                }

            }

        }

 

        private void SendMessageCompleted(IAsyncResult result)

        {

            InstantMessagingFlow instantMessagingFlow = result.AsyncState as InstantMessagingFlow;

            Exception ex = null;

            try

            {

                instantMessagingFlow.EndSendInstantMessage(result);

                Console.WriteLine("The message has been sent.");

            }

            catch (OperationTimeoutException opTimeEx)

            {

                // OperationFailureException: Indicates failure to connect the 

                // IM to the remote party due to timeout (called party failed to

                // respond within the expected time).

                // TODO (Left to the reader): Write real error handling code.

                ex = opTimeEx;

            }

            catch (RealTimeException rte)

            {

                // Other errors may cause other RealTimeExceptions to be thrown.

                // TODO (Left to the reader): Write real error handling code.

                ex = rte;

            }

            finally

            {

                // Reset the composing state of the local end user so that the typing notifcation as seen 

                // by the far end client disappears.

                _instantMessagingFlow.LocalComposingState = ComposingState.Idle;

                if (ex != null)

                {

                    // If the action threw an exception, terminate the sample, 

                    // and print the exception to the console.

                    // TODO (Left to the reader): Write real error handling code.

                    Console.WriteLine(ex.ToString());

                    Console.WriteLine("Shutting down platform due to error");

                    _helper.ShutdownPlatform();

                }

            }

        }

        #endregion

    }

}

And I have helper file for platform and endpoint creation

解决方案

Have you looked at the Contact Center reference application that ships with the UCMA 3.0 SDK?


这篇关于如何编写Web服务:处理外部事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆