Extending WCF using IServiceBehavior, IOperationBehavior, and IParameterInspector
[ServiceContract(Name = "PasswordGenerator")]
public interface IPasswordGenerator
{
[OperationContract(Name = "GeneratePassword")]
string GeneratePassword();
[OperationContract(Name="ParameterizedPasswordGenerator")]
string GeneratePassword(int length);
} public class PasswordGenerator:IPasswordGenerator
{
[OperationBehavior()]
public string GeneratePassword()
{
return "You called GeneratePassword()";
} [OperationBehavior()]
public string GeneratePassword(int length)
{
return "You called GeneratePassword(int length) overload";
}
}
static void Main(string[] args)
{
ServiceEndpoint serviceEndpoint;
Uri basePipeAddress = new Uri(@"net.Pipe://localhost/Password/Mine");
using (ServiceHost host =
new ServiceHost(typeof(Password.PasswordGenerator), basePipeAddress))
{
serviceEndpoint = host.AddServiceEndpoint(typeof(
Password.IPasswordGenerator), new NetNamedPipeBinding(), string.Empty);
host.Open(); using (var factory = new
ChannelFactory<password.passwordgenerator>(new NetNamedPipeBinding()))
{
var proxy = factory.CreateChannel(serviceEndpoint.Address);
proxy.GeneratePassword();
proxy.GeneratePassword(1500);
factory.Close();
}
host.Close();
}
}
public static class Logger
{
private static void Log(string message)
{
try
{
using (var stream = new StreamWriter(@"G:\log.log", true))
{
stream.WriteLine(message); stream.Flush();
stream.Close();
}
}
catch (Exception)
{ }
}
public static void Log(string className, string methodName, string parameter)
{
Log(string.Format("Class {0} called method {1} with parameter {2} @ {3}",
className, methodName, parameter, DateTime.Now));
} }
First Extension
public class MyServiceBehaviorAttribute : Attribute, IServiceBehavior
{ public void AddBindingParameters(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints,
BindingParameterCollection bindingParameters)
{
Logger.Log("MyServiceBehaviorAttribute",
"AddBindingParameters", serviceDescription.Name);
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
{
Logger.Log("MyServiceBehaviorAttribute",
"ApplyDispatchBehavior", serviceDescription.Name);
}
public void Validate(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
{
Logger.Log("MyServiceBehaviorAttribute",
"Validate", serviceDescription.Name);
}
}
Second Extension
public class MyOperationBehavior:Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription,
System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
Logger.Log("MyOperationBehavior",
"AddBindingParameters", operationDescription.Name);
} public void ApplyClientBehavior(OperationDescription operationDescription,
System.ServiceModel.Dispatcher.ClientOperation clientOperation)
{
clientOperation.ParameterInspectors.Add(new MyParameterInspector());
Logger.Log("MyOperationBehavior",
"ApplyClientBehavior", operationDescription.Name);
} public void ApplyDispatchBehavior(OperationDescription operationDescription,
System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(new MyParameterInspector());
Logger.Log("MyOperationBehavior",
"ApplyDispatchBehavior", operationDescription.Name);
} public void Validate(OperationDescription operationDescription)
{
Logger.Log("MyOperationBehavior", "Validate", operationDescription.Name);
}
}
Third Extension
class MyParameterInspector:IParameterInspector
{
public void AfterCall(string operationName, object[] outputs,
object returnValue, object correlationState)
{
Logger.Log("MyParameterInspector", "AfterCall", operationName);
} public object BeforeCall(string operationName, object[] inputs)
{
Logger.Log("MyParameterInspector", "BeforeCall", operationName);
return null;
}
}
[__strong__][MyServiceBehaviorAttribute()]
public class PasswordGenerator:IPasswordGenerator
{
[OperationBehavior(),MyOperationBehavior]
public string GeneratePassword()
{
return "You called GeneratePassword()";
} [OperationBehavior()]
[MyOperationBehavior]
public string GeneratePassword(int length)
{
return "You called GeneratePassword(int length) overload";
}
---------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Reflection; namespace AOPAndWCF
{
class MyParameterInspector: IOperationBehavior, IParameterInspector
{
#region IOperationBehavior Members
/// <summary>
///
/// </summary>
/// <param name="operationDescription"></param>
/// <param name="bindingParameters"></param>
public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{ }
/// <summary>
///
/// </summary>
/// <param name="operationDescription"></param>
/// <param name="clientOperation"></param>
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{ }
/// <summary>
///
/// </summary>
/// <param name="operationDescription"></param>
/// <param name="dispatchOperation"></param>
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(this);
} /// <summary>
///
/// </summary>
/// <param name="operationDescription"></param>
public void Validate(OperationDescription operationDescription)
{ } #endregion /// <summary>
/// 调用方法后 输出结果值
/// </summary>
/// <param name="operationName"></param>
/// <param name="outputs"></param>
/// <param name="returnValue"></param>
/// <param name="correlationState"></param>
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
Console.WriteLine("*************返回操作名称:" + operationName+"*************");
Console.WriteLine("*************返回操作编号:" + correlationState.ToString() + "**************");
for (int i = 0; i < outputs.Length; i++)
{ Type T = outputs[i].GetType();
Console.WriteLine("返回操作参数" + i.ToString() + " 类型为:" + T.ToString());
Console.WriteLine("返回操作参数" + i.ToString() + " ToString为:" + outputs[i].ToString());
Console.WriteLine("返回操作参数" + i.ToString() + " 属性:");
PropertyInfo[] PIs = T.GetProperties();
foreach (PropertyInfo PI in PIs)
{
Console.Write(PI.Name + ":");
Console.WriteLine(PI.GetValue(outputs[i], null));
} } Type Treturn = returnValue.GetType();
Console.WriteLine("操作返回值" + " 类型为:" + Treturn.ToString());
Console.WriteLine("操作返回值" + " ToString为:" + Treturn.ToString());
Console.WriteLine("操作返回值" + " 属性:"); if (Treturn.ToString() != "System.String")
{
PropertyInfo[] PIreturns = Treturn.GetProperties();
foreach (PropertyInfo PI in PIreturns)
{
Console.Write(PI.Name + ":");
Console.WriteLine(PI.GetValue(returnValue, null));
}
} }
/// <summary>
/// 调用方法前 输出参数值
/// </summary>
/// <param name="operationName"></param>
/// <param name="inputs"></param>
/// <returns></returns>
public object BeforeCall(string operationName, object[] inputs)
{
Guid guid = Guid.NewGuid(); Console.WriteLine("*************调用操作名称:" + operationName+"**************");
Console.WriteLine("*************调用操作编号:" + guid.ToString () + "**************");
for (int i = 0; i < inputs.Length ; i++)
{ Type T = inputs[i] .GetType ();
Console.WriteLine("操作参数"+i.ToString ()+" 类型为:"+T.ToString ());
Console.WriteLine("操作参数" + i.ToString() + " ToString为:" + inputs[i].ToString());
Console.WriteLine("操作参数" + i.ToString() + " 属性:");
PropertyInfo [] PIs = T.GetProperties();
foreach (PropertyInfo PI in PIs)
{
Console.Write ( PI.Name +":");
Console.WriteLine (PI.GetValue(inputs[i], null));
} }
return guid;
} }
}
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel ;
using System.ServiceModel.Description;
namespace AOPAndWCF
{
public class MyServiceHost:ServiceHost
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="serviceType">服务类型</param>
/// <param name="baseAddresses">基地址</param>
public MyServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { } /// <summary>
/// 应用配置文件设置
/// </summary>
protected override void ApplyConfiguration()
{
base.ApplyConfiguration();
//加入参数检查服务
addguidvalidation(); } /// <summary>
///加入参数检查服务
/// </summary>
private void addguidvalidation()
{
//加入参数检查服务
int endpointscount = this.Description.Endpoints.Count;
MyParameterInspector mypi = new MyParameterInspector();
for (int i = 0; i < endpointscount; i++)
{
if (this.Description.Endpoints[i].Contract.Name != "IMetadataExchange")
{
int Operationscount = this.Description.Endpoints[i].Contract.Operations.Count;
for (int j = 0; j < Operationscount; j++)
{
this.Description.Endpoints[i].Contract.Operations[j].Behaviors.Add(mypi);
}
}
}
} }
}
using System;
using System.Collections.Generic;
using System.Text; namespace AOPAndWCF
{
class Program
{
static void Main(string[] args)
{
MyServiceHost msh = new MyServiceHost(typeof(WcfServiceLibrary.Service1 ));
msh.Open();
Console.WriteLine ("服务已开启,回车关闭服务");
Console.ReadLine();
msh.Close(); }
}
}
Extending WCF using IServiceBehavior, IOperationBehavior, and IParameterInspector的更多相关文章
- Wcf实现IServiceBehavior拓展机制
IServiceBehavior接口 描述:提供一种在整个服务内修改或插入自定义拓展机制: 命名空间: System.ServiceModel.Description程序集: System.Ser ...
- 真实世界:使用WCF扩展记录服务调用时间
WCF 可扩展性 WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Layer 之上还提供了一个高级运行时,主要是针对应用程序开发人员.在 WCF 文档中,它常被称为服 ...
- WCF扩展
WCF 可扩展性 WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Layer 之上还提供了一个高级运行时,主要是针对应用程序开发人员.在 WCF 文档中,它常被称为服 ...
- 使用WCF扩展记录服务调用时间
随笔- 64 文章- 0 评论- 549 真实世界:使用WCF扩展记录服务调用时间 WCF 可扩展性 WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Lay ...
- WCF扩展记录服务调用时间
WCF 提供了许多扩展点供开发人员自定义运行时行为. WCF 在 Channel Layer 之上还提供了一个高级运行时,主要是针对应用程序开发人员.在 WCF 文档中,它常被称为服务模型层(Serv ...
- WCF笔记
http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.aftercall ...
- 转 使用IParameterInspector, IOperationBehavior,Attribute(参数检查器、操作行为接口和标签)扩展WCF操作行为
public class EntryIdInspector : IParameterInspector { public int intParamIndex { get; set; } string ...
- IServiceBehavior IContractBehavior IEndpointBehavior IOperationBehavior
using System; using System.Collections.ObjectModel; using System.Reflection; using System.ServiceMod ...
- WCF自定义扩展,以实现aop!
引用地址:https://msdn.microsoft.com/zh-cn/magazine/cc163302.aspx 使用自定义行为扩展 WCF Aaron Skonnard 代码下载位置: S ...
随机推荐
- Mac OS -bash: psql: command not found 使用 psql 命令报错
使用 psql 在 mac os 系统上登录,系统显示没有 psql 这个命令存在 解决方法如下: 将 postgresql 的 bin 目录添加到环境变量中即可 export PATH=" ...
- 获取并打印Spring容器中所有的Bean名称
思路: 1.实现Spring的ApplicationContextAware接口,重写setApplicationContext方法,将得到的ApplicationContext对象保存到一个静态变量 ...
- source insight 使用配置(私人)
1.输入两个空格,两个空格全消失,前后的字粘在一起显示,不想这样,就取消下图的勾.
- Egret中图片颜色的改变,颜色矩阵
参考: 图片处理:颜色矩阵和坐标变换矩阵 Egret-滤镜 之前面试有问到如何改变图片的颜色.貌似之前做Flash的时候做过,做Egret后没有此类需求,所以一直没有研究过. 现在来弄一弄如何改变图片 ...
- 转:HR schema
###createe RemRem $Header: hr_cre.sql 29-aug-2002.11:44:03 hyeh Exp $RemRem hr_cre.sqlRemRem Copyrig ...
- python中异常处理之esle,except,else
异常是指程序中的例外,违例情况.异常机制是指程序出现错误后,程序的处理方法.当出现错误后,程序的执行流程发生改变,程序的控制权转移到异常处理. python中使用try...except语句捕获异常, ...
- ABAP函数篇2 测试DATE_CONVERT_TO_FACTORYDATE
DATE_CONVERT_TO_FACTORYDATE 根据日期返回工厂日历日期 函数功能说明: 标出工作日的计算方法 输入传输 CORRECT_OPTION = '+'如果指定的日期不是工作日, ...
- Spring Boot与MyBatis的集成
SSM(Spring+Spring MVC+MyBatis)是当前主流的框架组合开发方式之一,普遍被应用于互联网项目中.如果要使用Spring Boot开发一个基于SSM框架的应用,那么我们要怎么做呢 ...
- array_map
<?php //对数组中的每个元素做函数处理 $arr = array(,,,,,); function cheng($hah){ ; } var_dump(array_map('cheng', ...
- 【超分辨率】—(ESRGAN)增强型超分辨率生成对抗网络-解读与实现
一.文献解读 我们知道GAN 在图像修复时更容易得到符合视觉上效果更好的图像,今天要介绍的这篇文章——ESRGAN: Enhanced Super-Resolution Generative Adve ...