我最近图书馆负责人:Fody。据我所知,它可以钩入构建过程并将IL注入程序集。我不完全确定它是如何工作的,但是可以通过搜索IL来找到所有具有ExposeToWeb
属性的方法,并使用它来将WCF服务的合约发送到程序集中。
但是另一方面,如果您已经为该类添加属性,为什么不直接添加正确的WFC属性开始,然后使用SvcUtil
在后期构建中生成合约?
编辑: 这里是你如何可以使用一个例子svcutil
:
C#:
[ServiceContract]
public interface IRainfallMonitor
{
[OperationContract]
void RecordRainfall(string county, float rainfallInches);
}
public class RainfallMonitor : IRainfallMonitor
{
public void RecordRainfall(string county, float rainfallInches)
{
// code
}
}
后生成的PowerShell:
$svcutil = "C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\SvcUtil.exe"
$csc = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
$assembly = "bin/debug/ProjectWithoutWCF.dll"
$service = "ProjectWithoutWCF.RainfallMonitor"
$outputns = "ProjectWithoutWCF.RainfallMonitor.Service"
$outputdir = "bin/debug"
md svcutil_tmp
cd svcutil_tmp
& $svcutil /serviceName:"$service" "../$assembly"
& $svcutil *.wsdl *.xsd /importxmltypes /out:"output.cs" /n:"*,$outputns"
& $csc /target:library /out:$outputns.dll "output.cs"
cp "$outputns.dll" "../$outputdir"
cp output.config "../$outputdir/$outputns.dll.config"
cd ..
rm -r .\svcutil_tmp
,您将需要在这样的事情你项目配置:
<system.serviceModel>
<services>
<service name="ProjectWithoutWCF.RainfallMonitor" >
<endpoint address="" binding="basicHttpBinding" contract="ProjectWithoutWCF.IRainfallMonitor">
</endpoint>
</service>
</services>
</system.serviceModel>
它有点烦琐,你很可能需要对脚本和配置进行一些调整。但结果是你有一个ProjectWithoutWCF.RainfallMonitor.Service.dll
文件与WCF服务合同。
我可以想到两个潜在的问题:1)方法重载不能映射到1个多个OperationContracts(名称必须不同)。 2)API中使用的所有复杂类型必须是可序列化的(例如[DataContracts]),因此API作者必须知道他们的API将作为WCF服务公开。 – nodots