我想使用" BluetoothLEAdvertisementWatcher"在WCF服务中
public override void StartWathchingAsync()
{
BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();
}
但是当我创建" BluetoothLEAdvertisementWatcher"的实例时我收到以下错误
类型&System; Service.ServiceModel.FaultException`1'的未处理异常发生在mscorlib.dll
其他信息:参数不正确。 (例外 HRESULT:0x80070057(E_INVALIDARG))
ExceptionDetail,可能由。创建 IncludeExceptionDetailInFaults = true,其值为: System.ArgumentException:参数不正确。 (例外 HRESULT:0x80070057(E_INVALIDARG))at MeasuringDeviceService.MeasuringDeviceClasseis.XiaomiMiScale.StartWathchingAsync() 在MeasuringDeviceService.DeviceService.GetData(MeasuringDevice measuringDevice)在C:\ Users \ Programmer \ Desktop \ Project中 MahdKodak \ MahdKodak \ MeasuringDeviceService \ DeviceService.svc.cs:行 37点 MeasuringDeviceService.DeviceService.GetDataXiaomiMiScale(XiaomiMiScale xiaomiMiScale)在C:\ Users \ Programmer \ Desktop \ Project中 MahdKodak \ MahdKodak \ MeasuringDeviceService \ DeviceService.svc.cs:行 25在SyncInvokeGetDataXiaomiMiScale(Object,Object [],Object []) 在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object 实例,对象[]输入,对象[]&输出) System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc&安培; rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
当我评论下面的代码行时,我的程序运作良好
//BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();
编辑:
这是我的代码
服务代码:
public class DeviceService : IDeviceService
{
Semaphore DeviceSemaphor = null;
public DeviceService()
{
DeviceSemaphor = new Semaphore(1, 1);
}
public XiaomiMiScaleData GetDataXiaomiMiScale(XiaomiMiScale xiaomiMiScale)
{
return GetData(xiaomiMiScale) as XiaomiMiScaleData;
}
public MeasuringDeviceData GetData(MeasuringDevice measuringDevice)
{
try
{
measuringDevice.StartWathchingAsync();
return measuringDevice.LastData;
}
catch (Exception ex)
{
return null;
}
}
}
其他课程:
[DataContract]
public class MeasuringDevice
{
[DataMember]
public MeasuringDeviceData LastData { get; set; }
public MeasuringDevice()
{
}
[DataMember]
public ulong DeviceBluetoothAddress { get; set; }
public virtual void StartWathchingAsync()
{
}
}
[DataContract]
public class XiaomiMiScale : MeasuringDevice
{
public XiaomiMiScale()
{
LastData = new XiaomiMiScaleData();
}
public override void StartWathchingAsync()
{
BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();
}
}
答案 0 :(得分:0)
问题出在MeasuringDeviceService.MeasuringDeviceClasseis.XiaomiMiScale.StartWathchingAsync()或者MeasureDeviceService.DeviceService.GetData(MeasuringDevice measuringDevice)中导致异常被抛出的地方。
您在堆栈中看到WCF的原因是因为上面的代码正在WCF服务中执行,并且当错误发生时,异常将通过FaultException发送回客户端。
我不知道您的代码或系统,但我的第一个猜测是您需要弄清楚DeviceService.GetData()的作用以及如何让它以独立的方式工作。
编辑:尝试运行BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();在WCF领域之外的独立方法中调用。
当你这样做时,它是否按预期工作?你能在服务器端放一个断点,看看究竟抛出了什么异常并粘贴了callstack吗?