简体   繁体   中英

C# - Using Windows.winmd in WCF Service

I want to use "BluetoothLEAdvertisementWatcher" in WCF Service

public override void StartWathchingAsync()
        {
            BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();
        }

But when I create instances of "BluetoothLEAdvertisementWatcher" I get the error below

Error Image

An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll

Additional information: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) at MeasuringDeviceService.MeasuringDeviceClasseis.XiaomiMiScale.StartWathchingAsync() at MeasuringDeviceService.DeviceService.GetData(MeasuringDevice measuringDevice) in C:\\Users\\Programmer\\Desktop\\Project MahdKodak\\MahdKodak\\MeasuringDeviceService\\DeviceService.svc.cs:line 37 at MeasuringDeviceService.DeviceService.GetDataXiaomiMiScale(XiaomiMiScale xiaomiMiScale) in C:\\Users\\Programmer\\Desktop\\Project MahdKodak\\MahdKodak\\MeasuringDeviceService\\DeviceService.svc.cs:line 25 at SyncInvokeGetDataXiaomiMiScale(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at 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.ServiceMo del.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)

And when I comment the code line below, my program work well

//BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();

EDIT :

This is my code

Service Code :

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;
            }
        }
    }

Other Classes :

[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();
        }
    }

The problem is either in MeasuringDeviceService.MeasuringDeviceClasseis.XiaomiMiScale.StartWathchingAsync() or somewhere in MeasuringDeviceService.DeviceService.GetData(MeasuringDevice measuringDevice) that's causing an exception to be thrown.

The reason why you see WCF in the stack is because the above code is being executed inside a WCF service and when the error occurs, the exception is being sent back to the client by means of a FaultException.

I don't know your code or system, but my first guess is you need to figure out what DeviceService.GetData() does and how to get it to work in a standalone manner.

EDIT: Try running BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher(); call in a standalone method outside the realm of WCF.

When you do so, does it work as expected? Can you put a breakpoint on your server side and see exactly what exception is being thrown and paste the callstack?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM